Java Reference
In-Depth Information
return name;
}
public double getSalary() {
return salary;
}
public boolean isManager() {
return manager;
}
}
You discuss your changes with the team head. “Great,” you're told, “this is exactly what we need!”
Throughout the following months, the HR department requests more and more features. “We forgot
to mention, we also want to keep track of the languages a programmer knows. Oh, and for manag-
ers, we want to keep track of the number of years they have been working at the company. Oh, and
we also have a third type of employee that we want to add. . .” You keep expanding and changing
your Employee class, and all is well in the world, right? Not so much. By now, your object‐oriented
senses should start tingling. The right approach to tackle this problem is to add an abstraction by
creating separate Employee , Manager , and Programmer classes, like so:
import java.util.ArrayList;
import java.util.List;
public abstract class Employee {
private List<Employee> employees;
private String name;
private double salary;
public Employee(String name, double salary) {
this.employees = new ArrayList<>();
this.name = name;
this.salary = salary;
}
public void addEmployee(Employee employee) {
employees.add(employee);
}
public void removeEmployee(Employee employee) {
employees.remove(employee);
}
public Employee getEmployee(int i) {
return employees.get(i);
}
public int getNrEmployees() {
return employees.size();
}
public String getName() {
return name;
}
Search WWH ::




Custom Search