How Polymorphism Works
Although classes inherit the attributes and methods of their superclasses, an inherited method may be redefined for a class to allow it to manipulate its own defined attributes. For example, assume you have created the user class hierarchy shown in the following illustration:
Every user class in the illustration has the attributes defined at its own level, as shown in the box for each user class, and all attributes defined by its superclasses. TempEmployee's attributes, therefore, are identical to Employee's, whereas Manager and PermanentEmployee define additional attributes.
Methods defined for each class are illustrated by a brace to the right of the class. For example, the Terminate method is defined only for the Employee class and inherited by all subclasses. The Hire method is defined for the Employee class and redefined at the level of each subclass.
The Terminate method is defined at the top of the hierarchy because terminating employees involves the same operation for all classes.
The Hire method inserts appropriate entries in database tables. In addition, the Hire method for PermanentEmployee inserts such information as NextReviewDate and InsurancePlan. The Hire method of the Manager class inserts the same information as the PermanentEmployee Hire method, but also enters information into the BonusRate column.
To cause a method to supersede the functions defined at the superclass level, you use the same name for the method (in this case, Hire), but you redefine the procedure in the user class script for the appropriate subclasses. The process of a subclass's method superseding the behavior of an identically-named method defined for a superclass is called polymorphism.
Last modified date: 06/25/2024