Subclass:
public class Manager extends Employee {
private double bonus;
private ArrayList<Employee> team;
public Manager(String firstName, String lastName, ArrayList<Employee> team) {
// using "super" as constructor
super(firstName, lastName);
// using "super" as variable
super.bonus = 0.2;
// using "this" as variable
this.bonus = 0.3;
this.team = team;
}
public Manager(String firstName, String lastName) {
// using "this" as constructor
this(firstName, lastName, new ArrayList<Employee>());
}
@Override
public double getSalary() {
// using "super" as method
return super.getSalary() * (1 + this.bonus);
}
}
Superclass:
public class Employee {
private String firstName;
private String lastName;
protected double bonus = 0.25;
private double salary;
public Employee(String firstName, String lastName) {
// using "this" as variable.
this.firstName = firstName;
this.lastName = lastName;
this.salary = 4000;
}
public double getSalary() {
return salary * (1 + this.bonus);
}
@Override
public String toString() {
return "Employee [firstName=" + firstName + ", lastName=" + lastName + ", salary=" + getSalary() + "]";
}
}
Comments
Leave a comment