Reputation:
I have a superclass (Employee) which implements an interface which contains only 1 method, as follows.
public interface Payable
{
double getPaymentAmount(); // calculate payment; no implementation
}
I've got a number of subclasses which inherit from Employee (e.g. SalariedEmployee, HourlyEmployee, CommissionEmployee), each of which contain a method earnings.
I've been asked to "can modify class Employee to implement interface Payable and declare method getPaymentAmount to invoke method earnings. Method getPaymentAmount would then be inherited by the subclasses in the Employee hierarchy. When getPaymentAmount is called for a particular subclass object, it polymorphically invokes the appropriate earnings method for that subclass".
How do I call the relevant method for earnings in the Employee class method getPaymentAmount without editing the subclasses?
I'm a relative newbie to Java.
The relevant part of the Employee class is as follows:
public abstract class Employee implements Payable
{
private String firstName;
private String lastName;
private String socialSecurityNumber;
// three-argument constructor
public Employee( String first, String last, String ssn )
{
firstName = first;
lastName = last;
socialSecurityNumber = ssn;
} // end three-argument Employee constructor
//getters, settters, toString override etc have been deleted.
public double getPaymentAmount()
{
???? //This is what I need help with.
}
} // end abstract class Employee
and taking 1 example of the subclass:
public class SalariedEmployee extends Employee
{
private double weeklySalary;
// four-argument constructor
public SalariedEmployee(String first, String last, String ssn, double salary)
{
super(first, last, ssn); // pass to Employee constructor
setWeeklySalary(salary); // validate and store salary
} // end four-argument SalariedEmployee constructor
@Override
public double earnings()
{
return getWeeklySalary();
} // end method earnings
} // end class SalariedEmployee
Upvotes: 0
Views: 726
Reputation: 6745
I think what you're looking for is something like this:
abstract class Employee implements Payable
{
double getPaymentAmount(){
return earnings();
}
abstract double earnings();
}
class SalariedEmployee extends Employee
{
double earnings(){
//Different implementation for different employee tpye
return xxx;
}
}
Since you have declared an abstract
method called earnings
, other methods of the abstract class
can call that method because they know that any instantiated instance of Employee
must have an implemented earnings
method.
Upvotes: 0
Reputation: 1071
Employee.java
=======================================================================
interface Payable
{
double getPaymentAmount(); // calculate payment; no implementation
}
public abstract class Employee implements Payable{
public double getPaymentAmount()
{
return 0.0;
}
public void printSalary()
{
}
}
Teacher.java
=======================================================================
public class Teachers extends Employee {
public double getPaymentAmount()
{
return 5;
}
public void printSalary()
{
System.out.println("Teachers current salary is: " + getPaymentAmount());
}
}
SoftwareEngineer.java
=======================================================================
public class SoftwareEngineer extends Employee {
public double getPaymentAmount()
{
return 500;
}
public void printSalary()
{
System.out.println("Software Engineers current salary is: " + getPaymentAmount());
}
}
TestEmployee.java
=======================================================================
public class TestEmployee {
public static void main(String s[])
{
Employee e = new Teachers();
e.printSalary();
Employee e1 = new SoftwareEngineer();
e1.printSalary();
}
}
Upvotes: -1
Reputation: 17454
Maybe this is what you looking for? To achieve polymorphic behaviour, let your various Employee classes have different implementation for earnings()
/ getPaymentAmount()
. Doing so will cause methods to override its superclass, achieving polymorphism.
class Employee implements Payable
{
double getPaymentAmount(){
return earnings();
}
double earnings(){
//Your other implementations
return xxx;
}
}
class SalariedEmployee extends Employee
{
double getPaymentAmount(){
return earnings();
}
double earnings(){
//Different implementation for different employee tpye
return xxx;
}
}
"How do I call the relevant method for earnings in the Employee class method getPaymentAmount?"
There is no need to worry about that. Java will take care of that for you. That is the basic of polymorphism. According to the object's class, they will call the respective methods.
Upvotes: 0