In subclasses of HourlyEmployee and SalariedEmployee, we call super()to pass the "name" to the base class constructor. I have two questions:
Where does the variable name come from, is this a typo for the variable aName?
How does the call work setSalary()in these subclasses?
Does the Employee class extend the instance of the method setSalary(), but then inside the method exists aSalary=salary;where the salary is not inherited, since it is private or inheritance simply allows you to use the method setSalary()from the base class, so using it super()to transfer the name makes sense.
public class Employee {
private String name;
private double salary;
public Employee(String aName) {
name = aName;
}
public void setSalary(double aSalary) {
salary = aSalary;
}
public String getName() {
return name;
}
public double getSalary() {
return salary;
}
public double getWeeklySalary() {
return salary/52;
}
}
public class HourlyEmployee extends Employee {
public HourlyEmployee(String aName, double anHourlySalary) {
super(name);
setSalary(anHourlySalary*40*52);
}
}
public class SalariedEmployee extends Employee {
public SalariedEmployee(String aName, double anAnnualSalary) {
super(name);
setSalary(anAnnualSalary);
}
}
source
share