Java Reference
In-Depth Information
Chapter 4
• Introduce subclasses, inheritance of components, and overriding methods.
• See how to draw an object of a subclass.
• Learn about keyword super in calling superclass methods.
• Learn how to cast from a superclass to a subclass and vice versa.
• Discuss designing object-oriented programs.
• Introduce the final model of execution.
• Discuss abstract classes.
In Fig. 3.1, we presented a class Employee . We now make this class more realis-
tic by introducing several kinds of employee: the salaried and hourly employees
and the executive. The yearly compensation is calculated differently for each kind
of employee. Salaried employees and executives get a yearly salary, hourly
employees are paid by the hour, and executives get bonuses.
With our current programming tools, changing the class of Fig. 3.1 to take
three kinds of employees into account would get messy. We would have to add
several fields to the class, most of which would be used in only one case.
Everywhere, there would be tests to determine which kind of employee is being
processed and to process the employee accordingly. Further, later changes, say, to
add a new kind of employee, would be difficult to do correctly.
To help solve such problems, we introduce a new structuring mechanism: the
subclass . It is the subclass, together with its notions of inheritance and overrid-
ing of methods, that make object-programming really useful. Java programs —
including the Java API classes— make heavy use of subclasses. For example,
writing GUIs would be far more difficult without subclasses.
Activity 4-1.1
shows how
class Employee
would have to
be changed to
take different
employees into
account. Watch
Search WWH ::

Custom Search