Java Reference
In-Depth Information
if (d instanceof Executive) {
} else if (d instanceof HourlyEmployee) {
} else if (d instanceof OvertimeEmployee) {
} else if
The class hierarchy
Subclasses of subclasses
We could extend class HourlyEmployee of Fig. 4.3 in at least two ways. We
could have a subclass PartTimeEmployee —part-time employees work only part
time (so they do not get health and retirement benefits), and the number of hours
worked is restricted in some fashion. We could also have a subclass Overtime-
Employee . Overtime employees are allowed to work overtime, usually at 1.5
times their normal hourly pay.
A folder of class PartTimeEmployee appears in Fig. 4.6. In it, there is one
partition for each class: PartTimeEmployee at the bottom, its superclass Hour-
lyEmployee above that, and its superclass Employee above that. The overriding
rule for public methods (see Sec. 4.1.1) works for such folders; to find the
method to call (within the folder), always search the folder from bottom to top.
In this case, both Employee and HourlyEmployee are called superclasses of
PartTimeEmployee . A subclass inherits the components of all its superclasses.
Subclasses of subclasses appear often in object-oriented programming. In
fact, the hierarchy can go quite deep. For example, class JFrame , which you stud-
ied in Chap. 1 and is part of the GUI classes for Java, is a subclass of Frame ,
which is a subclass of Window , which is a subclass of Container , which is a sub-
class of Component , which is a subclass of Object . This hierarchy was designed
to create a flexible set of GUI classes.
Component Employee
Container Executive HourlyEmployee
Object PartTimeEmployee OvertimeEmployee
Figure 4.8:
The subclass tree
Search WWH ::

Custom Search