Java Reference
In-Depth Information
base-salaried commission employee: Bob Lewis
social security number: 444-44-4444
gross sales: $5,000.00; commission rate: 0.04;
base salary: $300.00
new base salary with 10% increase is: $330.00
earned $530.00
Employee 0 is a SalariedEmployee
Employee 1 is a HourlyEmployee
Employee 2 is a CommissionEmployee
Employee 3 is a BasePlusCommissionEmployee
Fig. 10.9 | Employee hierarchy test program. (Part 4 of 4.)
Creating the Array of Employee s
Line 33 declares employees and assigns it an array of four Employee variables. Line 36 as-
signs to employees[0] a reference to a SalariedEmployee object. Line 37 assigns to
employees[1] a reference to an HourlyEmployee object. Line 38 assigns to employees[2]
a reference to a CommissionEmployee object. Line 39 assigns to employee[3] a reference
to a BasePlusCommissionEmployee object. These assignments are allowed, because a Sal-
ariedEmployee is an Employee , an HourlyEmployee is an Employee , a CommissionEmploy-
ee is an Employee and a BasePlusCommissionEmployee is an Employee . Therefore, we can
assign the references of SalariedEmployee , HourlyEmployee , CommissionEmployee and
BasePlusCommissionEmployee objects to superclass Employee variables, even though Em-
ployee is an abstract class .
Polymorphically Processing Employee s
Lines 44-65 iterate through array employees and invoke methods toString and earn-
ings with Employee variable currentEmployee , which is assigned the reference to a differ-
ent Employee in the array on each iteration. The output illustrates that the specific
methods for each class are indeed invoked. All calls to method toString and earnings are
resolved at execution time, based on the type of the object to which currentEmployee re-
fers. This process is known as dynamic binding or late binding . For example, line 46 im-
plicitly invokes method toString of the object to which currentEmployee refers. As a
result of dynamic binding , Java decides which class's toString method to call at execution
time rather than at compile time . Only the methods of class Employee can be called via an
Employee variable (and Employee , of course, includes the methods of class Object ). A su-
perclass reference can be used to invoke only methods of the superclass —the subclass meth-
od implementations are invoked polymorphically .
Performing Type-Specific Operations on BasePlusCommissionEmployee s
We perform special processing on BasePlusCommissionEmployee objects—as we encoun-
ter these objects at execution time, we increase their base salary by 10%. When processing
objects polymorphically , we typically do not need to worry about the specifics , but to adjust
the base salary, we do have to determine the specific type of Employee object at execution
time . Line 49 uses the instanceof operator to determine whether a particular Employee
object's type is BasePlusCommissionEmployee . The condition in line 49 is true if the ob-
ject referenced by currentEmployee is a BasePlusCommissionEmployee . This would also
be true for any object of a BasePlusCommissionEmployee subclass because of the is-a rela-
 
Search WWH ::




Custom Search