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
subset 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 employees
may be further subdivided as diagrammed in Display 7.1.
Within Java, you can define a class called Employee that includes all employees
(salaried or hourly), and then use this class to define classes for hourly employees and
salaried employees. You can then, in turn, use classes such as HourlyEmployee to
define classes such as PartTimeHourlyEmployee , and so forth.
Display 7.2 shows our definition for the class Employee . This class is a pretty
ordinary class. What is interesting about this class 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 reason for defining the class Employee is so that we can define
derived classes for different kinds of employees.
Display 7.1
A Class Hierarchy
Employee
HourlyEmployee
SalariedEmployee
FullTimeHourlyEmployee
PartTimeEmployee
TechnicalStaff
Executive
Engineer
Technician
ClericalStaff
 
 
Search WWH ::




Custom Search