Java Reference
In-Depth Information
tionship a subclass has with its superclass. Lines 53-54 downcast currentEmployee from
type Employee to type BasePlusCommissionEmployee —this cast is allowed only if the ob-
ject has an is-a relationship with BasePlusCommissionEmployee . The condition at line 49
ensures that this is the case. This cast is required if we're to invoke subclass BasePlusCom-
missionEmployee methods getBaseSalary and setBaseSalary on the current Employee
object—as you'll see momentarily, attempting to invoke a subclass-only method directly on a
superclass reference is a compilation error.
Common Programming Error 10.3
Assigning a superclass variable to a subclass variable is a compilation error.
Common Programming Error 10.4
When downcasting a reference, a ClassCastException occurs if the referenced object at ex-
ecution time does not have an is-a relationship with the type specified in the cast operator.
If the instanceof expression in line 49 is true , lines 53-60 perform the special pro-
cessing required for the BasePlusCommissionEmployee object. Using BasePlusCommis-
sionEmployee variable employee , line 56 invokes subclass-only methods getBaseSalary
and setBaseSalary to retrieve and update the employee's base salary with the 10% raise.
Calling earnings Polymorphically
Lines 63-64 invoke method earnings on currentEmployee , which polymorphically calls
the appropriate subclass object's earnings method. Obtaining the earnings of the Sala-
riedEmployee , HourlyEmployee and CommissionEmployee polymorphically in lines 63-
64 produces the same results as obtaining these employees' earnings individually in lines
22-27. The earnings amount obtained for the BasePlusCommissionEmployee in lines 63-
64 is higher than that obtained in lines 28-30, due to the 10% increase in its base salary.
Getting Each Employee 's Class Name
Lines 68-70 display each employee's type as a String . Every object knows its own class and
can access this information through the getClass method, which all classes inherit from
class Object . Method getClass returns an object of type Class (from package ja-
va.lang ), which contains information about the object's type, including its class name.
Line 70 invokes getClass on the current object to get its class. The result of the getClass
call is used to invoke getName to get the object's class name.
Avoiding Compilation Errors with Downcasting
In the previous example, we avoided several compilation errors by downcasting an Employ-
ee variable to a BasePlusCommissionEmployee variable in lines 53-54. If you remove the
cast operator (BasePlusCommissionEmployee) from line 54 and attempt to assign Em-
ployee variable currentEmployee directly to BasePlusCommissionEmployee variable em-
ployee , you'll receive an “ incompatible types ” compilation error. This error indicates
that the attempt to assign the reference of superclass object currentEmployee to subclass
variable employee is not allowed. The compiler prevents this assignment because a Com-
missionEmployee is not a BasePlusCommissionEmployee the is-a relationship applies only
between the subclass and its superclasses, not vice versa.
 
Search WWH ::




Custom Search