abstract interface: a named software representation for an abstraction's behavior that is intended to be implemented by one or more classes.

It is sometimes desirable to derive a subclass from several classes in order to inherit both their data and methods. Unfortunately, Java does not allow multiple inheritance. The extends keyword allows the specification of only one parent class. However, interfaces, or abstract interfaces, let the programmer obtain the effect of multiple inheritance.

An interface is a class-like construct that contains only constants and abstract methods--but no attributes. An interface is similar to an abstract class, but an abstract class can contain constants and abstract methods as well as variables and concrete methods.

The syntax to declare an interface is as follows:

modifier interface InterfaceName 
    /* Constant declarations */ 
    /* Method signatures */

An interface is treated like a special class in Java. Each interface is compiled into a separate bytecode file, just like a regular class. As with an abstract class, you cannot create an instance for the interface using the new operator, but in most cases you can use an interface more or less the same way you use an abstract class. For example, you can use an interface as a data type for a variable, as the result of casting, and so on.

Suppose you want to design a generic method to find the larger of two objects. The objects can be students, circles, or cylinders. Because compare methods are different for different types of objects, you need to define a generic compare method to determine the order of the two objects. Then you can tailor the method to compare students, circles, or cylinders. For example, you can use student ID as the key for comparing students, radius as the key for comparing circles, and volume as the key for comparing cylinders. 

The Comparable interface is part of the Java standard class library. The purpose of the interface is to provide a common mechanism for comparing one object to another. It contains only one method, compareTo, which accepts an object as a parameter and returns an integer.

The Comparable interface is defined as follows in java.lang:

// Interface for comparing objects, defined in java.lang 
public interface Comparable
    public int compareTo (Object obj);

This method is called by one object and is passed another object as a parameter. The compareTo method determines the order of the calling object with the specified object obj, and returns 

The compareTo behaves in a way similar to the compareTo for String objects.

A generic
max method for finding the larger of two objects can be declared in a class named Max, as follows.  Note that the return type is Comparable, as is the type of the two arguments.  (Recall that you can use an interface as a data type for a variable.)

// Find a maximum object 
package interfaceExample;

public class Max
    /* Return the maximum between two objects */
    public Comparable max (Comparable obj1, Comparable obj2)
        if (obj1.compareTo (obj2) > 0) 
            return obj1;
            return obj2;

Max class contains the method named max. To use the max method to find the larger of two objects, the class from which those objects are instantiated must implement the Comparable interface. 

The following example demonstrates how the interface is used.  This program uses the max method to find a maximum circle given two circles.


An additional example...

Example Review

max method can be used to find the larger of two objects of the Comparable type. Any object whose class implements the Comparable interface is an instance of the Comparable type. The example creates the class ComparableCircle in order to demonstrate the generic max method. The additional example creates the class ComparableCylinder to show the versatility of the max method. 

compareTo methods are different for different types of objects. The interface Comparable is used to generalize common functionality and leave the detail for the subclasses to implement.

The keyword
implements in the ComparableCircle class indicates that ComparableCircle inherits all the data from the Comparable interface and implements the methods in the interface.

ComparableCircle class implements the compareTo method for comparing the radii of two circles.  In the supplemental example, ComparableCylinder class implements the compareTo method for comparing the cylinders based on their volumes.

The interface
Comparable is used as a data type for variables obj1 and obj2 in the max method, and for the variable obj in the compareTo method of ComparableCircle. When you invoke max (circle1, circle2), circle1 and circle2 (of type ComparableCircle) are passed to obj1 and obj2 (of type Comparable). This is implicit casting. Method max returns an object of the Comparable type, and this object's reference is assigned to the variable circle. To invoke the getRadius from the Circle class, you have to cast the Comparable variable circle into a Circle object, because getRadius is not defined in the Comparable interface:  ( (Circle) circle ).getRadius ( )

Interfaces provide a form of generic programming. Without interfaces, the previous example would require multiple inheritance to inherit both
Comparable and Circle at the same time.

Interfaces vs. Abstract Classes

Defining an interface is similar to defining an abstract class. There are, however, a few differences:

Java allows only single inheritance for class extension, but multiple extension for interfaces, i.e., a single class can implement multiple interfaces. For example,

public class NewClass extends BaseClass 
    implements Interface1, ..., InterfaceN
    { - }

An interface can inherit other interfaces using the extends keyword, as follows:

public interface NewInterface extends Interface1, ..., InterfaceN
    // constants and abstract methods

A class implementing NewInterface must implement the abstract methods defined in NewInterface, Interface1, ..., and InterfaceN. An interface can extend other interfaces, but not classes. A class, on the other hand, can extend its superclass and implement multiple interfaces.

Both interfaces and abstract classes can be used to generalize common features. How do you decide whether to use an interface or a class? In general, a strong is-a relationship that clearly describes a parent-child relationship should be modeled using classes. For example, since a staff member is a person, their relationship should be modeled using class inheritance. A weak is-a relationship, also known as an is-kind-of relationship, indicates that an object possesses a certain property . A weak is-a relationship can be modeled using interfaces. For example, all strings are comparable, so the
String class implements the Comparable interface. Interface names are usually adjectives. You can also use interfaces to circumvent single inheritance restriction if multiple inheritance is desired. In the case of multiple inheritance, you have to design one as a superclass, and the others as interfaces.

Example of Creating an Interface

Consider, for example, a control panel such as that on a car radio or blower.  Many control panels have an on/off button as well as a way to increase or decrease the volume.  If a device has a control panel you expect to be able to turn it on and off, to increase or decrease the volume, or to be able to read its current value. This is true no matter how the device actually works or what other behavior it might provide. In fact, by this description, a heater or air conditioner might have similar controls, as might computer speakers. 

When creating an interface, be sure to remember the following:

Here is an example of a user-defined interface, including the interface, the class that implements it, and a main routine to test the implementation.

Final Notes

Similar to a class, an interface captures the behavior of some abstraction by defining a collection of methods. Unlike a class, the interface does not provide an implementation of the behavior; the implementation is provided by any class that defines and implements the behavior specified by the interface. 

An interface captures the behavior of an abstraction and not its attributes. Further, because an interface contains no implementation, it has no private section; all of the methods in an interface are public methods.

If an object is instantiated from a class that implements an interface, then that interface can be used to manipulate the object. The behavior defined and implemented by the class includes (or may be a superset of) the behavior defined by the interface. 

Several classes can implement the same interface, which means that objects of those classes can be manipulated through the same, single interface. 

A class that implements an interface MUST contain all the methods in the interface.



These notes provide additional information.