Java Reference
In-Depth Information
The Salary and Hourly classes already contain computePay(), so we can
declare computePay() abstract in Employee without modifying Salary or
Hourly. If we write a new class named Contractor that extends Employee, the
Contractor must override computePay() or be declared abstract. If we want to
instantiate Contractor objects, our only option is to override computePay(), as
demonstrated by the following Contractor class in Listing 8.16.
public class Contractor extends Employee
{
private double dailyRate;
private int daysWorked;
public Contractor(String name, String address, int number,
double dailyRate)
{
super(name, address, number);
setDailyRate(dailyRate);
}
public double computePay()
{
System.out.println(“Computing contractor pay for “
+ getName());
return dailyRate * daysWorked;
}
public void setDailyRate(double newRate)
{
if(newRate >= 0.0 && newRate <= 2000.00)
{
dailyRate = newRate;
}
}
public double getDailyRate()
{
return dailyRate;
}
public void setDaysWorked(int daysWorked)
{
if(daysWorked >= 0)
{
this.daysWorked = daysWorked;
}
}
}
Listing 8.16
The Contractor extends Employee and overrides the abstract computePay()
method.
Search WWH ::




Custom Search