3.3 Interfaces: The BlinkingObjects Applet

  1. The Purpose of Interfaces
  2. The Blinkable Interface
  3. Hierarchy of Interfaces

The Purpose of Interfaces

In our previous example, letters and digits were implemented as subclasses of the ScreenCharacter class. Screen characters can be drawn on the screen: in our case this behavior is implemented in the draw method of the ScreenCharacter class so that it can be applied to both letters and digits. Instance variable and other methods that letters and digits have in common are defined in the superclass as well.

Suppose that you want to implement blinking characters (for highlighting) in a class BlinkingCharacter. So you implement a BlinkingObject class that contains the methods startBlinking, stopBlinking, setBlinkingRate and getBlinkingRate for drawing a blinking object on the screen. You want to use these classes to build up the following applet of blinking letters, digits, ellipses, and rectangles.

It is natural and convenient to make BlinkingCharacter a subclass of BlinkingObject. It is also natural and convenient to make BlinkingCharacter a subclass of ScreenCharacter so that methods are inherited. So, the class hierarchy that you wish to use is the following:

Such a case when a class has more than one superclass, is called multiple inheritance in OOP jargon. However, Java does not allow multiple inheritance.

The designers of Java have made this decision because multiple inheritance is in practice error-prone: the most common difficulty is name clashing. Suppose for instance that both superclasses have an instance variable size. Which one is the "right" one in the subclass?

The Java developers choose the solution of interfaces to the problem of classifying common behavior.

An interface is a collection of method declarations (no implementations) and constant values.

"Implementing an interface" means promising to include the methods of the interface among the methods that are specific for the class under consideration.

The Blinkable Interface

The interface Blinkable could be defined by
interface Blinkable {

  void  startBlinking();

  void  stopBlinking();

  void  setBlinkingRate(float r);

  float getBlinkingRate();
}
By default, the methods in an interface are abstract and public. The first keyword indicates that no implementations are given and the second keyword provides maximal access.

To say that BlinkingCharacter is a subclass of ScreenCharacter and implements the interface Blinkable, you write

class BlinkingCharacter extends ScreenCharacter implements Blinkable {
  
  public void startBlinking () {...}

  public void stopBlinking () {...}

  public void setBlinkingRate (float r) { ... }

  public float getBlinkingRate () {
    return blinkingRate
  }
}
Here, we only sketch the implementation of the methods that have to do with the blinking rate. The keyword public is obligatory in the above definitions because otherwise the implementations in the BlinkingCharacter class would be less accessible than promised in the Blinkable interface (recall that methods declared in an interface are by default public).

If you extend the applet to also draw blinking rectangles and circles randomly on the screen with help of the above methods, then with the Blinkable interface you could in fact call the setBlinkingRate method without needing to know what object actually is to be drawn. All you need to know is that is implements the interface Blinkable. Code snippets could be as follows:

class Screen  {
  Blinkable b;
  int rate;

  ...

  b.setBlinkingRate(rate);

  ...

}
A graphical presentation:

Hierarchy of Interfaces

Interfaces also form a hierarchy. Because an interface only declares methods and does not implement them, multiple inheritance of interfaces can do no harm. So, the designers of Java allow you to introduce for example the Blinkable interface as an extension of, say, the Visible interface and the Invisible interface.

We give here an example, the BlinkingObjects, in which you can find the code for the applet, together with the interface. It uses some things that go behind the point we are now, so it is not part of the standard course.