Java Reference
In-Depth Information
Java provides a way out of this dilemma. To call the method
getCom-
pensation
in the superclass, prefix the call with
super
.
:
super
.getCompensation()
Notice that a reference
super
.salary
is illegal because field
salary
is pri-
vate. If it were not private,
super
.salary would be legal. But then the use of
“
super
.
” would be unnecessary.
Here is a step by step look at how the expression
super
.toString() + ", bonus " + bonus
of function
toString
in instance
a7
is evaluated.
(a) Evaluate
super
.toString()
, yielding the value
/**
An hourly employee
*/
public class
HourlyEmployee
extends
Employee {
/*
Class invariant:
salary = hourlyPay * hoursWorked */
/**
Amount payed per hour
and hours worked */
private double
hourlyPay;
private int
hoursWorked;
/**
Constructor: a person with name
n
, year hired
d
, hourly pay
p
, and hours worked
h*/
public
HourlyEmployee(String n,
int
d,
double
p,
int
h) {
super
(n, d);
hourlyPay= p;
hoursWorked= h;
super
.changeSalary(hourlyPay * hoursWorked);
}
/** =
the hourly pay of this employee
*/
public double
getHourlyPay()
{
return
hourlyPay; }
/** =
the hours worked by this employee
*/
public double
getHoursWorked()
{
return
hoursWorked; }
/** =
This method currently has no effect.
*/
public double
changeSalary(
double
d) { }
/** =
a representation of this hourly employee
*/
public
String toString() {
return super
.toString() + ", pay " + hourlyPay + ", hours " + hoursWorked;
}
}
Figure 4.3:
Subclass
HourlyEmployee
Search WWH ::
Custom Search