Java Reference
In-Depth Information
name of the interface to the end of your class declaration's first line. A class that does not
implement
all
the methods of the interface is an
abstract
class and must be declared
abstract
. Implementing an interface is like signing a
contract
with the compiler that states,
“I will declare all the methods specified by the interface or I will declare my class
abstract
.”
Common Programming Error 10.6
Failing to implement any method of an interface in a concrete class that
implements
the in-
terface results in a compilation error indicating that the class must be declared
abstract
.
Relating Disparate Types
An interface is often used when
disparate
classes—i.e., classes that are not related by a class
hierarchy—need to share common methods and constants. This allows objects of
unrelat-
ed
classes to be processed
polymorphically
—objects of classes that implement the
same
in-
terface can respond to the
same
method calls. You can create an interface that describes the
desired functionality, then implement this interface in any classes that require that func-
tionality. For example, in the accounts payable application developed in this section, we
implement interface
Payable
in any class that must be able to calculate a payment amount
(e.g.,
Employee
,
Invoice
).
Interfaces vs. Abstract Classes
An interface is often used in place of an
abstract
class when there's no default implementation to
inherit
—that is, no fields and no default method implementations. Like
public
abstract
classes, interfaces are typically
public
types. Like a
public
class, a
public
interface must be
declared in a file with the same name as the interface and the
.java
filename extension.
Software Engineering Observation 10.7
Many developers feel that interfaces are an even more important modeling technology than
classes, especially with the new interface enhancements in Java SE 8 (see Section 10.10).
Tagging Interfaces
We'll see in Chapter 15, Files, Streams and Object Serialization, the notion of
tagging in-
terfaces
(also called
marker interfaces
)—empty interfaces that have
no
methods or constant
values. They're used to add
is-a
relationships to classes. For example, in Chapter 15 we'll
discuss a mechanism called
object serialization
, which can convert objects to byte represen-
tations and can convert those byte representations back to objects. To enable this mecha-
nism to work with your objects, you simply have to mark them as
Serializable
by adding
implements
Serializable
to the end of your class declaration's first line. Then, all the
objects of your class have the
is-a
relationship with
Serializable
.
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 possibly unrelated
objects. After declaring interface
Payable
, we introduce class
Invoice
, which
implements