Java Reference
In-Depth Information
"Gries, year 1997, salary 50000"
(b) Catenate to it the string ", bonus " , giving thus far
"Gries, year 1997, salary 50000, bonus "
(c) Catenate to it the value of field bonus , giving the result
"Gries, year 1997, salary 50000, bonus 25000"
Functions getCompensation and toString in Fig. 4.1 make use of key-
word super to call inherited methods.
4.1.2
The class invariant
Figure 4.3 shows a second subclass of Employee : HourlyEmployee . Hourly
employees are paid by the hour, so the class has two private fields to contain the
hourly pay and the number of hours worked. The class also has two getter meth-
ods for obtaining the values of these fields.
Handling the inherited salary field of this class is a bit tricky. The superclass
has a procedure changeSalary(d) whose purpose is to change the salary to d ,
but we can't allow that procedure to be used for hourly employees because their
salary is based on the hourly pay and the number of hours worked. Therefore, the
subclass has an overriding procedure changeSalary(d) that does nothing.
Now look near the top of the class in Fig. 4.3. There is a comment, which
we call the class invariant . It states the relationship that must always hold
between the fields of an instance:
Style Note
13.4
describing
variables
/* class invariant: salary = hourlyPay * hoursWorked */
a7
Employee
Employee(String, int )
getCompensation()
addToSalary( double )
toString() getName()
setName(String) getStart()
setStart( int )
name
Gries
1997
start
50,000
salary
HourlyEmployee
getHourlyPay()
getHoursWorked()
changeSalary( double d)
toString()
HourlyEmployee(String, int ,
double , int )
1600
hoursWorked
hourlyPay
15.00
Figure 4.4:
An instance of subclass HourlyEmployee
Search WWH ::




Custom Search