Java Reference
In-Depth Information
ClassA
MethodA()
MethodB()
ClassB
MethodB()
ClassC
MethodA()
MethodB()
Figure 16-1 Polymorphism in Class Inheritance
1. IftheobjectmakingthecallbelongstoClassA,thentheimplementationof
MethodA() in ClassA is used. In this case inheritance is not applied since
ClassAhasnosuperclassandMethodA()isimplementedwithintheclass.
2. IfanobjectofClassBcallsMethodA(),thentheimplementationinClassAis
used.ThegeneralruleisthatJavasearchesuptheinheritancehierarchyfor
the closest polymorphic method.
3. If an object of ClassC calls methodA() then MethodA() in ClassC is used.
Here again, no inheritance is applied since the method is implemented in
the object making the call.
Modeling with inheritance
Let's start with a simple programming problem: suppose that you were re-
quired to write an application that calculates the payroll for the salaried
employees of a company. In addition, the company occasionally employs
part-timehelpwhichispaidbythehour.Atthesametime,muchofthedata
and many of the calculations that relate to regular employees also apply to
part-time employees. Since the part-time employees are a kind-of em-
 
Search WWH ::




Custom Search