G.12.6. Using Interface Payable to Process Invoices and Employees Polymorphically
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 ...
Get Android™ How to Program, Second Edition now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.