Reputation: 35
My assignment:
Code a class called SeniorWorker
which inherits from Employee
class, in addition to name
, salary
, and overtimePay
, it adds a double
type data called meritPay
which is calculated as 10% of the salary. It overrides earning()
and toString
methods to compute the total salary of SeniorWorker
and return proper data of SeniorWorker
, respectively.
Clarification: The assignment lets us use some example code from the book (Employee,RegularWorker,Manager, SeniorManager. java). My problem is that the SeniorWorker
class inherits from the Employee
class all those variable however the Employee
class only includes the name variable and the rest of the variables except the meritPay
are in the RegularWorker
Class that why I'm wondering if I could inherit from both the Employee
class and RegularWorker
or do I need to do something else?
Code:
public class Employee {
private String name;
// Constructor
public Employee(String name ) {
this.name = name;
}
public String getName()
{ return name; }
public double earnings(){return 0.0;}
}
public class RegularWorker extends Employee {
protected double salary, overtimePay;
public RegularWorker( String name, double salary, double overtimePay) {
super( name ); // call superclass constructor
this.salary = salary;
this.overtimePay = overtimePay;
}
public double earnings() { return salary + overtimePay; } //override the method to return salary
public String toString() { //override the method to print the name
return "Regular worker: " + getName();
}
}
My Class: So far i've just resorted to inheriting from the regularworker class(and it works) , however the assignment says i should inherit from employee class.
public class SeniorWorker extends RegularWorker {
public double meritPay;
public SeniorWorker(String name, double salary,double overtimePay, double meritPay)
{
super(name,salary,overtimePay);
this.meritPay = meritPay;
}
public double getMeritPay() {
return meritPay;
}
public void calculateMeritPay(){
meritPay = 0.10 * salary;
}
public double earnings() { return salary + overtimePay + meritPay; } //override the method to return salary
public String toString() { //override the method to print the name
return "Senior Worker: " + getName();
}
}
Upvotes: 2
Views: 234
Reputation: 16140
No, in java you can only inherit from one class. However, that class can inherit from another, and so on. You can extend multiple interfaces, which in java 8 could contain default method implementations (which could be default getters that return a predetermined value for the encapsulated attributes).
However, inheritance should only really be used for code sharing, and interfaces should only really be used for polymorphism.
Having said that, in your example, your RegularWorker is an Employee, so your SeniorWorker is a RegularWorker and is an Employee. Therefore, your SeniorWorker is inheriting (is extended from) Employee, as your assignment requests.
But, if you think about it, how can a senior worker also be a regular worker? They're not regular, they're senior. They may have been regular at one point in their career, but now they're senior, and not regular at all, despite having some stuff in common with regular employees. If you push the stuff from regularemployee up to employee, and have two descendants of employee, regular and senior, then you'll have sorted this mess out and probably earned extra points for being clever.
Upvotes: 1