"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.
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:
/* class invariant: salary = hourlyPay * hoursWorked */
Employee(String, int )
addToSalary( double )
setStart( int )
changeSalary( double d)
HourlyEmployee(String, int ,
double , int )
An instance of subclass HourlyEmployee