Java Reference
In-Depth Information
Using Interfaces
What you have seen up to now has primarily illustrated the mechanics of creating an interface and incorpor-
ating it into a class. The really interesting question is — what should you use interfaces for?
An interface that declares methods defines a standard set of operations. Different classes can add such a
standard interface by implementing it. Thus, objects of a number of different class types can share a com-
mon set of operations. Of course, a given operation in one class may be implemented quite differently from
how it is implemented in another class. But the way in which you invoke the operation is the same for ob-
jects of all class types that implement the interface. For this reason it is often said that an interface defines a
contract for a set of operations.
I hinted at the third and perhaps most important use of interfaces at the beginning of this discussion. An
interface defines a type, so you can expedite polymorphism across a set of classes that implement the same
interface. This is an extremely useful and powerful facility. Let's have a look at how this works.
Interfaces and Polymorphism
You can't create objects of an interface type, but you can create a variable of an interface type. For example:
Conversions converter = null; // Variable of the Conversions interface type
If you can't create objects of type Conversions , what good is it? Well, you use it to store a reference
to an object of any class type that implements Conversions . This means that you can use this variable to
call the methods declared in the Conversions interface polymorphically. The Conversions interface is not
a good example to show how this works. Let's consider a real-world parallel that I can use to better demon-
strate this idea, that of home audio/visual equipment and a remote control. I'm grateful to John Ganter who
suggested this idea to me after reading a previous edition of this topic.
You have a TV, possibly an audio receiver, and almost certainly a DVD player around your home, and
each of them has its own remote control. All the remote controls probably have some common subset of
buttons — power on/off, volume up, volume down, mute, and so on. After you have more than four or so
remotes cluttering the place up, you might consider one of those fancy universal remote control devices to
replace them — sort of a single definition of a remote control, to suit all equipment.
A universal remote has a lot of similarities to an interface. By itself a universal remote does nothing. It
defines a set of buttons for standard operations, but the operation of each button must be programmed spe-
cifically to suit each kind of device that you want to control. You can represent the TV, VCR, DVD, and so
on by classes, each of which makes use of the same remote control interface — the set of buttons, if you
like — but each in a different way. Even though it uses the same button on the remote, Power On for the
TV, for example, is quite different from Power On for the VCR. Let's see how that might look in a concrete
example.
TRY IT OUT: Defining Interfaces
Here's how you might define an interface to model a simple universal remote:
Search WWH ::




Custom Search