Java Reference
In-Depth Information
earnings
toString
firstName lastName
social security number:
SSN
Employee
abstract
salaried employee:
firstName lastName
social security number:
SSN
weekly salary:
weeklySalary
Salaried-
Employee
weeklySalary
if (hours <= 40)
wage * hours
else if (hours > 40)
{
40 * wage +
( hours - 40 ) *
wage * 1.5
}
hourly employee:
firstName lastName
social security number:
SSN
hourly wage:
wage
; hours worked:
hours
Hourly-
Employee
commission employee:
firstName lastName
social security number:
SSN
gross sales:
grossSales
;
commission rate:
commissionRate
Commission-
Employee
commissionRate *
grossSales
base salaried commission employee:
firstName lastName
social security number:
SSN
gross sales:
grossSales
;
commission rate:
commissionRate
;
base salary:
baseSalary
BasePlus-
Commission-
Employee
(commissionRate *
grossSales) +
baseSalary
Fig. 10.3
|
Polymorphic interface for the
Employee
hierarchy classes.
The following sections implement the
Employee
class hierarchy of Fig. 10.2. The first
section implements
abstract superclass
Employee
. The next four sections each implement
one of the
concrete
classes. The last section implements a test program that builds objects
of all these classes and processes those objects polymorphically.
Class
Employee
(Fig. 10.4) provides methods
earnings
and
toString
, in addition to the
get
methods that return the values of
Employee
's instance variables. An
earnings
method
certainly applies
generically
to all employees. But each earnings calculation depends on the
employee's particular class. So we declare
earnings
as
abstract
in superclass
Employee
because a
specific
default implementation does not make sense for that method—there isn't
enough information to determine what amount
earnings
should return.
Each subclass overrides
earnings
with an appropriate implementation. To calculate an
employee's earnings, the program assigns to a superclass
Employee
variable a reference to the
employee's object, then invokes the
earnings
method on that variable. We maintain an
array of
Employee
variables, each holding a reference to an
Employee
object. You
cannot
use
class
Employee
directly to create
Employee
objects
, because
Employee
is an
abstract
class. Due
to inheritance, however, all objects of all
Employee
subclasses may be thought of as
Employee