7 Polymorphism
7 Polymorphism
Day
Day day;
day = new Holiday();
Holiday
day.celebrate();
StaffMember
Volunteer Employee
Executive Hourly
//----------------------------------------------------------
// Constructor: Sets up this staff member using the
// specified information.
//----------------------------------------------------------
public StaffMember (String eName, String eAddress,
String ePhone)
{
name = eName;
address = eAddress;
phone = ePhone;
}
return result;
}
//----------------------------------------------------------
// Derived classes must define the pay method for each type
// of employee.
//----------------------------------------------------------
public abstract double pay();
}
//----------------------------------------------------------
// Returns a zero pay value for this volunteer.
//----------------------------------------------------------
public double pay()
{
return 0.0;
}
}
//----------------------------------------------------------
// Constructor: Sets up this employee with the specified
// information.
//----------------------------------------------------------
public Employee (String eName, String eAddress,
String ePhone, String socSecNumber,
double rate)
{
super (eName, eAddress, ePhone);
socialSecurityNumber = socSecNumber;
payRate = rate;
}
return result;
}
//----------------------------------------------------------
// Returns the pay rate for this employee.
//----------------------------------------------------------
public double pay()
{
return payRate;
}
}
//----------------------------------------------------------
// Constructor: Sets up this executive with the specified
// information.
//----------------------------------------------------------
public Executive (String eName, String eAddress,
String ePhone, String socSecNumber,
double rate)
{
super (eName, eAddress, ePhone, socSecNumber, rate);
//----------------------------------------------------------
// Computes and returns the pay for an executive, which is
// the regular employee payment plus a one-time bonus.
//----------------------------------------------------------
public double pay()
{
double payment = super.pay() + bonus;
bonus = 0;
return payment;
}
}
//----------------------------------------------------------
// Constructor: Sets up this hourly employee using the
// specified information.
//----------------------------------------------------------
public Hourly (String eName, String eAddress, String ePhone,
String socSecNumber, double rate)
{
super (eName, eAddress, ePhone, socSecNumber, rate);
hoursWorked = 0;
}
//----------------------------------------------------------
// Computes and returns the pay for this hourly employee.
//----------------------------------------------------------
public double pay()
{
double payment = payRate * hoursWorked;
hoursWorked = 0;
return payment;
}
return result;
}
}
//----------------------------------------------------------
// Constructor: Sets up the list of staff members.
//----------------------------------------------------------
public Staff ()
{
staffList = new StaffMember[6];
((Executive)staffList[0]).awardBonus (500.00);
((Hourly)staffList[3]).addHours (40);
}
if (amount == 0.0)
System.out.println ("Thanks!");
else
System.out.println ("Paid: " + amount);
System.out.println ("------------------------------");
}
}
}
personnel.payday();
}
}
Speaker current;
current.speak();