Java Reference
In-Depth Information
If we 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 methods declared in the Conversions interface polymorphically. The Conversions is not a
good example to show how this works. Let's consider a real world parallel that we can use to better
demonstrate 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 almost certainly have a TV, a hi-fi, a VCR, and maybe a DVD player, around your home, and each of
them will have its own remote control. All the remote controls will probably have some common subset of
buttons, power on/off, volume up, volume down, mute, and so on. Once you have more than four or so
remotes cluttering the place up, you might consider one of these 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
specifically to suit each kind of device that you want to control. We can represent the TV, VCR, DVD,
etc. by classes, each of which will make 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 instance 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 we might define an interface to model a simple universal remote:
public interface RemoteControl {
boolean powerOnOff(); // Returns new state, on = true
int volumeUp(int increment); // Returns new volume level
int volumeDown(int decrement); // Returns new volume level
void mute(); // Mutes sound output
int setChannel(int channel); // Set the channel number and return it
int channelUp(); // Returns new channel number
int channelDown(); // Returns new channel number
}
The methods declared here in the RemoteControl interface should be self-explanatory. We have
included just a few of the many possible remote operations here to conserve space in the topic. You
could add more if you want. You could have separate power on and off methods for instance, tone
controls, and so on. There is no definition for any of these methods here. Methods declared in an
interface are always abstract - by definition. There is also no access attribute for any of them.
Methods declared in an interface are always public by default.
Now any class that requires the use of the functionality provided by a RemoteControl just has to
declare that it implements the interface, and include the definitions for each of the methods in the
interface. For example, here's the TV:
public class TV implements RemoteControl {
public TV(String make, int screensize) {
this.make = make;
this.screensize = screensize;
Search WWH ::




Custom Search