10.5.6 Polymorphic Processing, Operator instanceof and Downcasting
To test our Employee
hierarchy, the application in Fig. 10.9 creates an object of each of the four concrete classes SalariedEmployee
, HourlyEmployee
, CommissionEmployee
and BasePlusCommissionEmployee
. The program manipulates these objects nonpolymorphically, via variables of each object’s own type, then polymorphically, using an array of Employee
variables. While processing the objects polymorphically, the program increases the base salary of each BasePlusCommissionEmployee
by 10%—this requires determining the object’s type at execution time. Finally, the program polymorphically determines and outputs the type of each object in the Employee
array. Lines 9–18 create objects of ...
Get Java™ How To Program (Early Objects), Tenth Edition now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.