Java Reference
In-Depth Information
In the Payroll class, the payEmployee() method uses the Payable
parameter to invoke computePay() and mailCheck(). The Payroll class
cannot invoke any of the methods in Employee that do not appear in
Payable. For example, within payEmployee(), the statement:
p.setName(“Bill Gates”); //Will not compile
is not valid. Only the three methods in Payable can invoked using p
because p is a Payable reference. This was the whole purpose of this
example, to create a situation in which specific methods of the Employee
class are exposed through an interface. The way the Payroll class is
designed, Payroll objects cannot access or change an employee's name or
address. Payroll objects can only invoke the three methods in Payable that
pertain to an employee's weekly paycheck.
The following PayableDemo program instantiates a Payroll object and pays
an Employee. Study the program and try to determine its output, which is
shown in Figure 10.8.
public class PayableDemo
{
public static void main(String [] args)
{
Employee e = new Employee(“George Washington”, “Mt. Vernon”);
Payroll payroll = new Payroll();
payroll.payEmployee(e);
payroll.printPaycheck(e);
}
}
Figure 10.8
The output of the PayableDemo program.
Search WWH ::




Custom Search