Java Reference
In-Depth Information
overridden, the
Volunteer
class would have been considered abstract and could
not have been instantiated.
Note that when a volunteer gets “paid” in the
payday
method of
Staff
, a
simple expression of thanks is printed. In all other situations, where the pay value
is greater than zero, the payment itself is printed.
The
Employee
class shown in Listing 10.5 represents an employee that gets paid
at a particular rate each pay period. The pay rate, as well as the employee's Social
Security number, is passed along with the other basic information to the
Employee
constructor. The basic information is passed to the constructor of
StaffMember
using the
super
reference.
The
toString
method of
Employee
is overridden to concatenate the additional
information that
Employee
manages to the information returned by the parent's
version of
toString
, which is called using the
super
reference. The
pay
method
of an
Employee
simply returns the pay rate for that employee.
The
Executive
class shown in Listing 10.6 represents an employee that may
earn a bonus in addition to his or her normal pay rate. The
Executive
class
is derived from
Employee
and therefore inherits from both
StaffMember
and
Employee
. The constructor of
Executive
passes along its information to the
Employee
constructor and sets the executive bonus to zero.
A bonus is awarded to an executive using the
awardBonus
method. This
method is called in the
Staff
constructor for the only executive that is part of the
staffList
array. Note that the generic
StaffMember
reference must be cast into
an
Executive
reference to invoke the
awardBonus
method (which doesn't exist
for a
StaffMember
).
The
Executive
class overrides the
pay
method so that it first determines
the payment as it would for any employee, then adds the bonus. The
pay
method
of the
Employee
class is invoked using
super
to obtain the normal payment
amount. This technique is better than using just the
payRate
variable, because
if we choose to change how
Employee
objects get paid, the change will auto-
matically be reflected in
Executive
. After the bonus is awarded, it is reset
to zero.
The
Hourly
class shown in Listing 10.7 represents an employee whose pay rate
is applied on an hourly basis. It keeps track of the number of hours worked in the
current pay period, which can be modified by calls to the
addHours
method. This
method is called from the
payday
method of
Staff
. The
pay
method of
Hourly
determines the payment based on the number of hours worked and then resets
the hours to zero.
Search WWH ::
Custom Search