Java Reference
In-Depth Information
However, the descendants of
StaffMember
each provide their own specific definition
for
pay
. By defining
pay
abstractly in
StaffMember
, the
payday
method of
Staff
can polymorphically pay each employee.
This is the essence of polymorphism. Each class knows best how it should
handle a specific behavior, in this case paying an employee. Yet in one sense it's
all the same behavior—the employee is getting paid. Polymorphism lets us treat
similar objects in consistent but unique ways.
The
Volunteer
class shown in Listing 10.4 represents a person that is not
compensated monetarily for his or her work. We keep track only of a volunteer's
basic information, which is passed into the constructor of
Volunteer
, which in
turn passes it to the
StaffMember
constructor using the
super
reference. The
pay
method of
Volunteer
simply returns a zero pay value. If
pay
had not been
LISTING 10.4
//********************************************************************
// Volunteer.java Author: Lewis/Loftus
//
// Represents a staff member that works as a volunteer.
//********************************************************************
public class
Volunteer
extends
StaffMember
{
//-----------------------------------------------------------------
// Constructor: Sets up this volunteer using the specified
// information.
//-----------------------------------------------------------------
public
Volunteer (String eName, String eAddress, String ePhone)
{
super
(eName, eAddress, ePhone);
}
//-----------------------------------------------------------------
// Returns a zero pay value for this volunteer.
//-----------------------------------------------------------------
public double
pay()
{
return
0.0;
}
}
Search WWH ::
Custom Search