Next Prev Up Contents


5 Interfaces


5.1 - Interfaces as Types
5.2 - Methods in Interfaces
5.3 - Variables in Interfaces
5.4 - Combining Interfaces
An interface specifies a collection of methods without implementing their bodies. Interfaces provide encapsulation of method protocols without restricting the implementation to one inheritance tree. When a class implements an interface, it generally must implement the bodies of all the methods described in the interface. (If the implementing class is abstract--never implemented--it can leave the implementation of some or all of the interface methods to its subclasses.)

Interfaces solve some of the same problems that multiple inheritance does without as much overhead at runtime. However, because interfaces involve dynamic method binding, there is a small performance penalty to using them.

Using interfaces allows several classes to share a programming interface without having to be fully aware of each other's implementation. The following example shows an interface declaration (with the interface keyword) and a class that implements the interface:

    public interface Storing {
        void freezeDry(Stream s); 
        void reconstitute(Stream s); 
    }
    public class Image implements Storing, Painting {
        ...
        void freezeDry(Stream s) { 
            // JPEG compress image before storing
            ... 
        }
        void reconstitute (Stream s) { 
            // JPEG decompress image before reading
            ... 
        }
    }
Like classes, interfaces are either private (the default) or public. The scope of public and private interfaces is the same as that of public and private classes, respectively. Methods in an interface are always public. Variables are public, static, and final.


5.1 Interfaces as Types

The declaration syntax interfaceName variableName declares a variable or parameter to be an instance of some class that implements interfaceName. Interfaces behave exactly as classes when used as a type. This lets the programmer specify that an object must implement a given interface, without having to know the exact type or inheritance of that object. Using interfaces makes it unnecessary to force related classes to share a common abstract superclass or to add methods to Object.

The following pseudocode illustrates the interfaceName variableName syntax:

    class StorageManager {
        Stream stream;
        ...
        // Storing is the interface name
        void pickle(Storing obj) { 
            obj.freezeDry(stream);
        }
    }

5.2 Methods in Interfaces

Methods in interfaces are declared as follows:

     returnType methodName ( parameterList );

The declaration contains no modifiers. All methods specified in an interface are public and abstract and no other modifiers may be applied.

See "Abstract Methods" on page 22 for more information on abstract methods.


5.3 Variables in Interfaces

Variables declared in interfaces are final, public, and static. No modifiers can be applied. Variables in interfaces must be initialized.


5.4 Combining Interfaces

Interfaces can incorporate one or more other interfaces, using the extends keyword as follows:

    interface DoesItAll extends Storing, Painting {
        void doesSomethingElse();
    }

Next Prev Up Contents

The Java Language Specification

Generated with CERN WebMaker