Previous Page Next Page Start of Tutorial > Start of Trail > Start of Lesson Lesson Contents

Trail:
Learning the Java Language
Lesson:
More Features of the Java Language



Help on searching
is available.

Search technology
by Excite, Inc.

Warning! Interfaces Cannot Grow

Suppose you want to add some functionality to AlarmClock for its 2.0 release. For example, GUIClock needs to update its display every minute so that it makes continual requests to be woken up. Really, GUIClock just wants AlarmClock to beep at it every minute. It would be preferable to register a single request with AlarmClock, for two reasons:
  • It's more efficient because it involves fewer method calls.
  • GUIClock wouldn't have to maintain its own AlarmClock because it wouldn't risk losing its bed.
AlarmClock needs to differentiate such "beeps" from an actual wake up call. So now the Sleeper interface must include a beep method:
public interface Sleeper {
    public void wakeUp();
    public void beep();

    public long ONE_SECOND = 1000;
    public long ONE_MINUTE = 60000;
}
However, if you make this change to Sleeper, all classes that implement the old Sleeper will break because they don't implement the interface anymore! Programmers relying on this interface will protest loudly.

Try to anticipate all uses for your interface up front and specify it completely from the beginning. Given that this is often impossible, you may need either to create more interfaces later or to break your customer's code.

 
Previous Page Next Page Start of Tutorial > Start of Trail > Start of Lesson Lesson Contents