Java Reference
In-Depth Information
Implementing for Extensibility
With polymorphism, we can design and implement systems that are easily
extensible
—new
classes can be added with little or no modification to the general portions of the program,
as long as the new classes are part of the inheritance hierarchy that the program processes
generically. The new classes simply “plug right in.” The only parts of a program that must
be altered are those that require direct knowledge of the new classes that we add to the hier-
archy. For example, if we extend class
Animal
to create class
Tortoise
(which might re-
spond to a
move
message by crawling one inch), we need to write only the
Tortoise
class
and the part of the simulation that instantiates a
Tortoise
object. The portions of the sim-
ulation that tell each
Animal
to move generically can remain the same.
Chapter Overview
First, we discuss common examples of polymorphism. We then provide a simple example
demonstrating polymorphic behavior. We use superclass references to manipulate
both
su-
perclass objects and subclass objects polymorphically.
We then present a case study that revisits the employee hierarchy of Section 9.4.5. We
develop a simple payroll application that polymorphically calculates the weekly pay of sev-
eral different types of employees using each employee's
earnings
method. Though the
earnings of each type of employee are calculated in a
specific
way, polymorphism allows us
to process the employees “in the
general
.” In the case study, we enlarge the hierarchy to
include two new classes—
SalariedEmployee
(for people paid a fixed weekly salary) and
HourlyEmployee
(for people paid an hourly salary and “time-and-a-half” for overtime).
We declare a common set of functionality for all the classes in the updated hierarchy in an
“abstract” class,
Employee
, from which “concrete” classes
SalariedEmployee
,
HourlyEm-
ployee
and
CommissionEmployee
inherit directly and “concrete” class
BasePlusCommis-
sionEmployee
inherits indirectly. As you'll soon see,
when we invoke each employee's
earnings
method off a superclass
Employee
reference (regardless of the employee's type), the cor-
rect earnings subclass calculation is performed,
due to Java's polymorphic capabilities.
Programming in the Specific
Occasionally, when performing polymorphic processing, we need to program “in the
spe-
cific
.” Our
Employee
case study demonstrates that a program can determine the
type
of an
object at
execution
time
and act on that object accordingly. In the case study, we've decided
that
BasePlusCommissionEmployee
s should receive 10% raises on their base salaries. So,
we use these capabilities to determine whether a particular employee object
is a
Base-
PlusCommissionEmployee
. If so, we increase that employee's base salary by 10%.
Interfaces
The chapter continues with an introduction to Java
interfaces
, which are particularly useful
for assigning
common
functionality to possibly
unrelated
classes. This allows objects of
these classes to be processed polymorphically—objects of classes that
implement
the
same
interface can respond to all of the interface method calls. To demonstrate creating and us-
ing interfaces, we modify our payroll application to create a generalized accounts payable
application that can calculate payments due for company employees
and
invoice amounts
to be billed for purchased goods.