G.8. Demonstrating Polymorphic Behavior
Section G.4 created a class hierarchy, in which class
BasePlusCommissionEmployee inherited from
CommissionEmployee. The examples in that section manipulated
BasePlusCommissionEmployee objects by using references to them to invoke their methods—we aimed superclass variables at superclass objects and subclass variables at subclass objects. These assignments are natural and straightforward—superclass variables are intended to refer to superclass objects, and subclass variables are intended to refer to subclass objects. However, as you’ll soon see, other assignments are possible.
In the next example, we aim a superclass reference at a subclass object. We then show how invoking a method ...