Java Reference
In-Depth Information
53
return String.format( "%s %s%n%s: %.2f" , "base-salaried" ,
super .toString(), "base salary" , getBaseSalary());
54
55
}
56
} // end class BasePlusCommissionEmployee
Fig. 9.11 | BasePlusCommissionEmployee class inherits from CommissionEmployee and
accesses the superclass's private data via inherited public methods. (Part 2 of 2.)
Class BasePlusCommissionEmployee 's earnings Method
Method earnings (lines 43-47) overrides class CommissionEmployee 's earnings method
(Fig. 9.10, lines 87-90) to calculate a base-salaried commission employee's earnings. The
new version obtains the portion of the earnings based on commission alone by calling Com-
missionEmployee 's earnings method with super.earnings() (line 46), then adds the
base salary to this value to calculate the total earnings. Note the syntax used to invoke an
overridden superclass method from a subclass—place the keyword super and a dot ( . ) sep-
arator before the superclass method name. This method invocation is a good software en-
gineering practice—if a method performs all or some of the actions needed by another
method, call that method rather than duplicate its code. By having BasePlusCommission-
Employee 's earnings method invoke CommissionEmployee 's earnings method to calcu-
late part of a BasePlusCommissionEmployee object's earnings, we avoid duplicating the
code and reduce code-maintenance problems .
Common Programming Error 9.2
When a superclass method is overridden in a subclass, the subclass version often calls the
superclass version to do a portion of the work. Failure to prefix the superclass method name
with the keyword super and the dot ( . ) separator when calling the superclass's method
causes the subclass method to call itself, potentially creating an error called infinite recur-
sion, which would eventually cause the method-call stack to overflow—a fatal runtime
error. Recursion, used correctly, is a powerful capability discussed in Chapter 18.
Class BasePlusCommissionEmployee 's toString Method
Similarly, BasePlusCommissionEmployee 's toString method (Fig. 9.11, lines 50-55)
overrides CommissionEmployee 's toString method (Fig. 9.10, lines 93-101) to return a
String representation that's appropriate for a base-salaried commission employee. The new
version creates part of a BasePlusCommissionEmployee object's String representation (i.e.,
the String "commission employee" and the values of class CommissionEmployee 's private
instance variables) by calling CommissionEmployee 's toString method with the expression
super.toString() (Fig. 9.11, line 54). BasePlusCommissionEmployee 's toString meth-
od then completes the remainder of a BasePlusCommissionEmployee object's String rep-
resentation (i.e., the value of class BasePlusCommissionEmployee 's base salary).
Testing Class BasePlusCommissionEmployee
Class BasePlusCommissionEmployeeTest performs the same manipulations on a Base-
PlusCommissionEmployee object as in Fig. 9.7 and produces the same output, so we do
not show it here. Although each BasePlusCommissionEmployee class you've seen behaves
identically, the version in Fig. 9.11 is the best engineered. By using inheritance and by call-
ing methods that hide the data and ensure consistency, we've efficiently and effectively
constructed a well-engineered class.
 
Search WWH ::




Custom Search