Java Reference
In-Depth Information
Derived Classes
Suppose we are designing a record-keeping program that has records for salaried
employees and hourly employees. There is a natural hierarchy for grouping these
classes. These are all classes of people who share the property of being employees.
Employees who are paid an hourly wage are one subset of employees. Another sub-
set consists of salaried employees who are paid a fixed wage each month. Although the
program may not need any type corresponding to the set of all employees, thinking in
terms of the more general concept of employees can be useful. For example, all
employees have a name and a hire date (when they started working for the company),
and the methods for setting and changing names and hire dates will be the same for
salaried and hourly employees. The classes for hourly employees and salaried employ-
ees may be further subdivided as diagrammed in Display 7.1.
Within Java you can define a class called
that includes all employees,
whether salaried or hourly, and then use this class to define classes for hourly employ-
ees and salaried employees. You can then, in turn, use classes like
Employee
to
HourlyEmployee
define classes like
, and so forth.
Display 7.2 shows our definition for the class
PartTimeHourlyEmployee
is a
pretty ordinary class much like earlier classes we have seen. What will be interesting
about the class
. The class
Employee
Employee
is how we use it to create a class for hourly employees and a
class for salaried employees. It is legal to create an object of the class
Employee
, but our
Employee
reason for defining the class
is so that we can define derived classes for differ-
Employee
ent kinds of employees.
Display 7.1 A Class Hierarchy
Employee
HourlyEmployee
SalariedEmployee
FullTimeHourlyEmployee
PartTimeEmployee
TechnicalStaff
Executive
Engineer
Technician
ClericalStaff
 
Search WWH ::




Custom Search