Why is my Polymorphic Parent class calling the Subclass Method from within itself?
Heres the code I've made up so far. Its fully functional and the only gripe I have with it is that my output for Weekly and Annual pay is always weekly...I'm at a loss as to how to get this from within either toString method.
public class PolyEmployees {
public static void main(String[] args) {
Employee [] myEmployees = {
new Hourly("Joan Rivers", "Human Resources", 12.45, 34.3),
new Hourly("Jason Nezbit", "Accounting", 15.25, 46.0),
new Hourly("Ingrid Homes", "Secretary", 10.11, 38.7),
new Salaried("Amy Liberman", "Human Resources Executive", 32.50),
new Salaried("Xander Xavar", "Resource Processing", 29.20),
new Salaried("Milly Rockhome", "PR Executive", 65.28)
};
// Output all employee types
for (int i = 0; i < myEmployees.length; i++) {
System.out.println("\n" + myEmployees[i].toString());
}
}
}
/*
* Employee abstract class
*/
abstract public class Employee {
private String mName;
private String mDepartment;
protected Double mRate;
// Constructor
public Employee(String mName, String mDepartment, Double mRate) {
this.mName = mName;
this.mDepartment = mDepartment;
this.mRate = mRate;
}
// Annual Pay
public Double pay() { // 40 Hours a Week, 52 weeks in a year
return ((this.mRate * 40) * 52);
}
@Override
public String toString() {
return "Employee: " + this.mName + "\nDepartment: " + this.mDepartment + "\nAnnual Pay: " + this.pay();
}
}
/*
* Hourly employee class
*/
public class Hourly extends Employee {
开发者_如何学编程 private Double mHours;
public Hourly(String mName, String mDepartment, Double mRate, Double mHours) {
super(mName, mDepartment, mRate);
this.mHours = mHours;
}
@Override
public Double pay() { // Weekly Pay, deals with overtime for hourly employee
if (this.mHours > 40.0) {
return ((40 * this.mRate) + ((this.mHours-40) * (this.mRate * 1.5)));
}
else {
return (this.mHours * this.mRate);
}
}
public String toString() {
return super.toString() + "\tWeekly Pay: " + pay();
}
}
/*
* Salaried Employee Class
*/
public class Salaried extends Employee{
public Salaried(String mName, String mDepartment, Double mRate) {
super(mName, mDepartment, mRate);
}
@Override
public Double pay() { // Weekly Pay
return (this.mRate * 40);
}
@Override
public String toString() {
return super.toString() + "\tWeekly Pay: " + this.pay();
}
}
I get all the output I want except for annual pay. Stepping through the debugger it returns to the childs pay method even when calling from within the parent. Since it's overridden I am not really surprised by this, but part of my deliverable is to get weekly from the subclass and annual from the super.
So that begs my question, how can I get Annual pay from the parent? Do I have no choice but to cast it as an employee as part of my system output or is there something I am missing?
And on a side note, I love how fluid this site is. Not many places I've been to show your post live as you type it.
NOTE ON COMMENT: According to my deliverables, both the salaried and hourly employees toString must return weekly pay. The employee abstract class itself contains the method to return the annual pay.
You have the option of calling the super from the subclass. Your toString
in Salaried
, for example, could look like this:
return super.toString() + "\tWeekly Pay: " + this.pay()+ "\tAnnual Pay: " + super.pay());
Yes, calling pay()
in Employee
will indeed call Salaried.pay()
in the context of a Salaried
object. That's the point of polymorphism - that derived classes can override behaviour.
Now it strikes me that your classes would be a lot clearer if you had separate methods for getAnnualPay()
and getWeeklyPay()
- possibly only having getAnnualPay()
in the base class, and introducing getWeeklyPay()
in Salaried
.
In your subclasses, you're overriding the pay() method. If you want the employees to have both an annual pay and a periodic pay, you should add abstract method periodicPay() for the subclasses to override and leave pay() as final.
精彩评论