How You Can Build an Inheritance Tree
The illustrated example in
How Polymorphism Works suggests that two of the subclasses (PermanentEmployee and Manager) define additional attributes at their own level, requiring the Hire method to behave differently for the superclass and its subclasses. The scenario is different if two subclasses define identical additional attributes.
Suppose that you create a class to represent contract employees. If the Hire method is the same for both temporary employees and contract employees, you could use the same source code for the Hire method in both the TempEmployee and ContractEmployee classes. However, this is a case where defining an intermediate abstract class may be more efficient. The following diagram shows how you can create an HourlyEmployee class that contains the Hire method for both TempEmployee and ContractEmployee:
You thus define the Hire, GetCurrent, and GetHistory methods, and the attributes they use, only once: for abstract class HourlyEmployee. The TempEmployee and ContractEmployee classes inherit these methods from HourlyEmployee.
Last modified date: 12/20/2023