Java Reference
In-Depth Information
boss.payEmployee(s);
System.out.println(“\n** Paying Hourly object **”);
boss.payEmployee(h);
System.out.println(“\n** Paying Employee object **”);
boss.payEmployee(e);
}
}
Listing 8.12
(continued)
Within the PayDemo2 program, a Salary, Hourly, and Employee object are
instantiated and passed in to the payEmployee() method of SmartBoss. (We can
pass each type in to payEmployee() because it has an Employee parameter.)
The payEmployee() method invokes computePay() which, by the nature of vir-
tual methods, invokes computePay() in the appropriate child class. The same
occurs when invoking mailCheck(), and the output is shown in Figure 8.5.
You should have a good reason to override a parent class method. Due to
the behavior of virtual method invocation, which is the default behavior
of all Java methods, overriding a method hides the parent class version
of the method. In most cases, that is the desired result; however, if you
just want to add to the tasks being performed in the parent method, be
sure to invoke the parent method using the super keyword in the child
method.
Figure 8.5
The output of the PayDemo2 program.
Search WWH ::




Custom Search