Java Reference
In-Depth Information
ployee,wecouldconceivablymodelthesystemusingclassinheritance.In
thiscaseyoucouldcreateasuperclasscalledEmployee,andasubclass
namedPartTimer.PartTimerwouldinheritallthemethodsofEmployee
andimplementsomeofitsown.Theclassdiagramisshownin Figure16-2 .
Employee
name
address
ssn
dependents
number
ShowData()
PrintCheck()
PartTimer
hourlyWage
hoursWorked
PrintCheck()
Figure16-2ACaseofClassInheritance
Inthediagramin Figure16-2 , assumethatthemethodPrintCheck()in
the class PartTimer has a different parameter list than PrintCheck() in the
class Employee. This is consistent with a real-world situation in which
the data required for a part-timer is typically different from the data for a
salaried employee. The result of this inheritance scheme is that if an ob-
ject of the PartTimer class calls the method ShowData(), then the method
in Employee is used. However, if a PartTimer object calls the method
PrintCheck() then its own implementation is used and not the one in the
Employee class. The program named SimpleInherit.java, listed next,
shows simple inheritance and method selection by overriding.
On the Web
ThesourcefilefortheprogramSimpleInherit.javacanbefoundinthe
Chapter16folderat www.crcpress.com .
 
Search WWH ::




Custom Search