Java, multiple inheritance. How can I do it?

I am creating a payroll system for employees. I have an abstract Employee class. WageEmployee and Manager continue with Employee . Programmer and SalesPerson extend WageEmployee .

My problem is that I want to create a SalesManager . SalesManger pays salaries by adding commission and salaries. Therefore, they are of type SalesPerson and Manager .

What should I do with the interface, and what SalesPerson do?

It is natural to extend the SalesManager from the manager, and then make the SalesPerson interface. But I can not, because it inherits from WageEmployee ...

How do I get this to work. right?

 abstract class Employee { String name; Employee() {} Employee (String nm) { name = nm; } abstract double computePay(); void display () {} void setHours(double hrs) {} void setSales(double sales) {} void setSalary(double salary) { System.out.println("NO!"); } 

}

+6
source share
3 answers

Call me crazy, but you may not need a subclass to deal with various payment methods here. Instead, consider using the Strategy to determine salary / commission sales. So you have the Employee class and its subclass SalesEmployee ; each of them will have the property of a PayStrategy object, which can have specific subclasses of WagePayStrategy and CommissionPayStrategy . If you define the correct methods on PayStrategy (it can be an interface or an abstract class), you can have Employee instances that define the strategy objects that the employee pays for certain resources (hours, sales, etc.) ..

Consider the following trivial example:

 interface PayStrategy { public float getPay(float hoursWorked, float salesMade); } class WageStrategy implements PayStrategy { public float getPay(float hoursWorked, float salesMade) { return hoursWorked * 8.5; } } class CommissionStrategy implements PayStrategy { public float getPay(float hoursWorked, float salesMade) { return salesMade * 0.5 + hoursWorked * 5.0; } } abstract class Employee { public PayStrategy payStrategy; // assign as needed public float getPay(float hoursWorked, float salesMade) { return this.payStrategy.getPay(hoursWorked, salesMade); } } 

Please note how you can change the payment strategy for any given employee without requiring another class, giving you more flexibility and encapsulation around this algorithm.

+10
source

Employee has getFixedSalary() and getComissionSalary() methods.

You can create abstract subclasses of SalariedEmployee ( getComissionSalary always returns 0) and make Programmer and SalesPerson deploy it. SalesManager implements Employee directly.

Or all classes directly inherit from Employee to provide maximum flexibility if the salary scale changes on one day (I would go this way). Write helper methods to avoid code duplication, but provided that you can still modify modus operandi if needed later.

+1
source

When you extend classes, you usually want to keep some kind of functionality, and you add it, expanding it. With an interface, it provides you with a list of methods that objects will have.

It looks like you have Employee and Manager s, and you need to decide what the functionality will expand from. Despite your code or object model, it looks like all Employee , and some of them are Manager . So it looks like you need to decide which additional manager does what makes it separate from Employee. Then everything will expand with Employee and expand with Employee subclasses ( WageEmployee , SalaryEmployee , etc.), and then those that are managers, they implement the Manager interface. That way you can have the SalesPerson extension WageEmployee , and then the SalesManager extension SalesPerson , which implements Manager .

+1
source

Source: https://habr.com/ru/post/920380/


All Articles