Java Reference
In-Depth Information
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
}
Directory "TryRemoteControl"
The methods declared here in the RemoteControl interface should be self-explanatory. I 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 power off methods, for example, 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. Nor is there an 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:
import static java.lang.Math.max;
import static java.lang.Math.min;
public class TV implements RemoteControl {
public TV(String make, int screensize) {
this.make = make;
this.screensize = screensize;
// In practice you would probably have more
// arguments to set the max and min channel
// and volume here plus other characteristics for a particular TV.
}
public boolean powerOnOff() {
power = !power;
System.out.println(make + " "+ screensize + " inch TV power "
+ (power ? "on.":"off."));
return power;
}
Search WWH ::




Custom Search