Java Reference
In-Depth Information
25
// output currentPayable and its appropriate payment amount
26
System.out.printf(
"%n%s %n%s: $%,.2f%n"
,
27
currentPayable.toString()
currentPayable.getPaymentAmount()
,
// could invoke implicitly
28
"payment due"
,
);
29
}
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
Fig. 10.15
|
Payable
interface test program processing
Invoice
s and
Employee
s
polymorphically. (Part 2 of 2.)
You'll use interfaces extensively when developing Java applications. The Java API contains
numerous interfaces, and many of the Java API methods take interface arguments and re-
turn interface values. Figure 10.16 overviews a few of the more popular interfaces of the
Java API that we use in later chapters.
Interface
Description
Java contains several comparison operators (e.g.,
<
,
<=
,
>
,
>=
,
==
,
!=
) that allow you
to compare primitive values. However, these operators
cannot
be used to compare
objects. Interface
Comparable
is used to allow objects of a class that
implements
the
interface to be compared to one another. Interface
Comparable
is commonly used
for ordering objects in a collection such as an array. We use
Comparable
in
Chapter 16, Generic Collections, and Chapter 20, Generic Classes and Methods.
Comparable
Fig. 10.16
|
Common interfaces of the Java API. (Part 1 of 2.)