PayableInterfaceTest
(Fig. G.27) illustrates that interface Payable
can be used to process a set of Invoice
s and Employee
s polymorphically in a single application. Line 9 declares payableObjects
and assigns it an array of four Payable
variables. Lines 12–13 assign the references of Invoice
objects to the first two elements of payableObjects
. Lines 14–17 then assign the references of SalariedEmployee
objects to the remaining two elements of payableObjects
. These assignments are allowed because an Invoice
is a Payable
, a SalariedEmployee
is an Employee
and an Employee
is a Payable
. Lines 23–29 use the enhanced for
statement to polymorphically process each Payable
object in payableObjects
, printing the object as a String
, along with the payment amount due. Line 27 invokes method toString
via a Payable
interface reference, even though toString
is not declared in interface Payable
—all references (including those of interface types) refer to objects that extend Object and therefore have a toString method. (Method toString
also can be invoked implicitly here.) Line 28 invokes Payable
method getPaymentAmount
to obtain the payment amount for each object in payableObjects
, regardless of the actual type of the object. The output reveals that the method calls in lines 27–28 invoke the appropriate class’s implementation of methods toString
and getPaymentAmount
. For instance, when currentPayable
refers to an Invoice
during the first iteration of the for
loop, class Invoice
’s toString
and getPaymentAmount
execute.
1 // Fig. G.27: PayableInterfaceTest.java
2 // Tests interface Payable.
3
4 public class PayableInterfaceTest
5 {
6 public static void main( String[] args )
7 {
8 // create four-element Payable array
9 Payable[] payableObjects = new Payable[ 4 ];
10
11 // populate array with objects that implement Payable
12 payableObjects[ 0 ] = new Invoice( "01234", "seat", 2, 375.00 );
13 payableObjects[ 1 ] = new Invoice( "56789", "tire", 4, 79.95 );
14 payableObjects[ 2 ] =
15 new SalariedEmployee( "John", "Smith", "111-11-1111", 800.00 );
16 payableObjects[ 3 ] =
17 new SalariedEmployee( "Lisa", "Barnes", "888-88-8888", 1200.00 );
18
19 System.out.println(
20 "Invoices and Employees processed polymorphically:
" );
21
22 // generically process each element in array payableObjects
23 for ( Payable currentPayable : payableObjects )
24 {
25 // output currentPayable and its appropriate payment amount
26 System.out.printf( "%s
%s: $%,.2f
",
27 currentPayable.toString(),
28 "payment due", currentPayable.getPaymentAmount() );
29 } // end for
30 } // end main
31 } // end class PayableInterfaceTest
Invoices and Employees processed polymorphically:
invoice:
part number: 01234 (seat)
quantity: 2
price per item: $375.00
payment due: $750.00
invoice:
part number: 56789 (tire)
quantity: 4
price per item: $79.95
payment due: $319.80
salaried employee: John Smith
social security number: 111-11-1111
weekly salary: $800.00
payment due: $800.00
salaried employee: Lisa Barnes
social security number: 888-88-8888
weekly salary: $1,200.00
payment due: $1,200.00