This feels like a missed opportunity for polymorphism.
This is where a bunch of classes share the same method signatures as their superclass/interface, so the code that calls it doesn't need to know which type it is.
Without polymorphism:
Employee employee = ...;
if(employee instanceof Doctor) {
salary = calcDoctorSalary(...);
} else if(employee instanceof Nurse) {
salary = calcNurseSalary(...);
}
With polymorphism:
Employee employee = ...;
salary = employee.calcSalary(...);
The magic goes into the subclasses. calcSalary()
is abstract in a superclass, or a method signature in an interface:
public abstract class Employee {
public abstract int calcSalary(...);
}
... or ...
public interface Employee {
public int calcSalary(...);
}
Then the type-dependent logic goes into the subclasses:
public class Nurse implements Employee {
@Override
public int calcSalary(...) {
// code specific to nurses goes here.
}
}
Whether to extend a class, or implement an interface, is something you'll learn with experience. Often when one doesn't start with an interface, one regrets it later.