To build an application that can determine payments for employees and invoices alike, we first create interface Payable
, which contains method getPaymentAmount
that returns a double
amount that must be paid for an object of any class that implements the interface. Method getPaymentAmount
is a general-purpose version of method earnings
of the Employee
hierarchy—method earnings
calculates a payment amount specifically for an Employee
, while getPaymentAmount
can be applied to a broad range of unrelated objects. After declaring interface Payable
, we introduce class Invoice
, which implements
interface Payable
. We then modify class Employee
such that it also implements interface Payable
. Finally, we update Employee
subclass SalariedEmployee
to “fit” into the Payable
hierarchy by renaming SalariedEmployee
method earnings
as getPaymentAmount
.
Classes Invoice
and Employee
both represent things for which the company must be able to calculate a payment amount. Both classes implement the Payable
interface, so a program can invoke method getPaymentAmount
on Invoice
objects and Employee
objects alike. As we’ll soon see, this enables the polymorphic processing of Invoice
s and Employee
s required for the company’s accounts payable application.
The UML class diagram in Fig. G.22 shows the hierarchy used in our accounts payable application. The hierarchy begins with interface Payable
. The UML distinguishes an interface from other classes by placing the word “interface” in guillemets (« and ») above the interface name. The UML expresses the relationship between a class and an interface through a relationship known as realization. A class is said to “realize,” or implement, the methods of an interface. A class diagram models a realization as a dashed arrow with a hollow arrowhead pointing from the implementing class to the interface. The diagram in Fig. G.22 indicates that classes Invoice
and Employee
each realize (i.e., implement) interface Payable
. As in the class diagram of Fig. G.14, class Employee
appears in italics, indicating that it’s an abstract class. Concrete class SalariedEmployee
extends Employee
and inherits its superclass’s realization relationship with interface Payable
.