1.6.1 Interfaces1.6 Abstract Data Types1.6 Abstract Data Types1.6.1.1 Polymorphism via Interfaces

1.6.1 Interfaces

Until now we have used the term interface to denote the public part of a class; this part consists of the methods by which we can interact with objects of the class. The interface hides the class implementation, i.e., the object fields which hold the contents of the object. This separation between interface and implementation is also supported by a special Java construct.

We separate the interface of a class from its implementation.

A Java  interface is a collection of class constants and  abstract object methods declared as follows:

  interface Name
  {
    static final type name = expression; // class constant
    type name(parameters);               // abstract method
    ...
  }

The Java identifier Name is the name of the interface; as a convention, we use names with upper-case capital letters. The keyword static final is redundant and may be omitted. Please note that the abstract method declaration does not contain a body.

Such an interface expresses the public part of a class that represents the type of an object. A class implements such an interface if it is declared as

  class Class implements Name
  {
    public type name(parameters)
    {
      ...
    }
  }

This class contains a method implementation for every abstract method declared in the interface. The method declaration in the class must have the same return type and parameter list as the declaration in the interface. The method must be declared with the keyword public; the reason will become clear later. The basic idea of this declaration is depicted in the following figure: objects that are of type Class can "hide" behind a "shield" represented by the interface type Name. The details will be explained in the following section.


Interface

Interface and Class
 


Example  Take the interface
  interface Container
  {
    static final int size = 100;
    void addElement(int x);
    int getElement(int i);
  }

This interface is implemented by the following class:

  class Array implements Container
  {
     int[] a = new int[size];
     int l = 0;

     void setElement(int i, int x)
     {
       a[i] = x;
     }
 
     public void addElement(int x)
     {
       setElement(l, x);
       l++;
     }

     public int getElement(int i)
     {
       return a[i];
     }
  }

Please note that the class implementation can refer to the class constant size declared in the interface. Furthermore, the implementation has fields a and l and a method setElement that are not visible in the interface.

A class may also implement multiple interfaces by a declaration of the form

  class Class implements Name1, Name2, ...
  {
    ...
  }

In this case, the class must provide a method for every abstract method in every interface listed in the class declaration.


© Wolfgang Schreiner; February 3, 2005

1.6.1 Interfaces1.6 Abstract Data Types1.6 Abstract Data Types1.6.1.1 Polymorphism via Interfaces