Java Reference
In-Depth Information
The Payroll class is a more generic and adaptable class than the
SmartBoss class. The SmartBoss can only pay Employee objects, which
limits it to child classes of Employee. The Payroll class can pay any
Payable object, which is any object whose class implements Payable. This
includes all Employee objects because Employee implements Payable.
Imagine that the Payroll Department needs to send checks to members of
the Board of Directors, who are represented by a Director class. If the
Director class does not extend Employee, the SmartBoss will not be able
to pay Director objects. However, if the Director class implements Payable:
public class Director implements Payable
Director objects can be passed in to the payEmployee() method of Payroll
without any modifications to the Payroll class. The payEmployee() method
does not know the actual data type of the Payable argument passed in. It
only knows that the argument is Payable, and therefore has a
computePay() and mailCheck() method.
The Payable interface demonstrates how an interface is used to expose
methods of a class. The Payroll class is handed Payable objects, not Employee
objects. Similarly, suppose that we have a class named HumanResources to
represent the Human Resources Department. If we want the human resources
department to only be able to invoke the methods of Employee that are
exposed in the EmployeeInfo interface, we can design the methods of the
HumanResources class so that they use EmployeeInfo references.
The following HumanResources class contains methods for changing an
employee's name and address. Notice that a HumanResources object cannot
invoke methods like computePay() or mailCheck() in Employee. It can only
invoke those methods in the EmployeeInfo interface.
public class HumanResources
{
public String getInfo(EmployeeInfo e)
{
return e.getName() + “ “ + e.getAddress();
}
public void changeName(EmployeeInfo e, String name)
Search WWH ::




Custom Search