Previous | Next | Trail Map | Writing Java Programs | Objects, Classes, and Interfaces


Defining an Interface

To create an interface, you must write both the interface declaration and the interface body.
interfaceDeclaration {
    interfaceBody
}
The interfaceDeclaration declares various attributes about the interface such as its name and whether it extends another interface. The interfaceBody contains the constant and method declarations within the interface.

The Interface Declaration

At minimum, the interface declaration contains the Java keyword interface and the name of the interface that you are creating:
interface Countable {
    . . .
}

Note: By convention, interface names begin with capital letters just like class names do. Often, interface names end with "able" or "ible."

An interface declaration can have two other components: the public access specifier and a list of superinterfaces. An interface can extend other interfaces just as a class can extend or subclass another class. However, while a class can only extend one other class, an interface can extend any number of interfaces. Thus, the full interface declaration looks like this:

[public] interface InterfaceName [extends listOfSuperInterfaces] {
    . . .
}

The public access specifier indicates that the interface can be used by any class in any package. If you do not specify that your interface is public, then your interface will only be accessible to classes that are defined in the same package as the interface.

The extends clause is similar to the extends clause in a class declaration, however, an interface can extend multiple interfaces (a class can only extend one), and an interface cannot extend classes. The list of superinterfaces is a comma-delimited list of all of the interfaces extended by the new interface. An interface inherits all constants and methods from its superinterface unless the interface hides a constant with another of the same name, or redeclares a method with a new method declaration.

The Interface Body

The interface body contains method declarations for the methods defined within the interface. Implementing Methods shows you how to write a method declaration. In addition to method declarations, an interface can contain constant declarations. See Declaring Member Variables for information about how to construct a member variable declaration.


Note: Member declarations in an interface disallow some of the use of some declaration modifiers and discourage the use of others. You may not use transient, volatile, or synchronized in a member declaration in an interface. Also, you may not use the private and protected specifiers when declaring members of an interface.

All constant values defined in an interface are implicitly public, static, and final. The use of these modifiers on a constant declaration in an interface is discouraged as a matter of style. Similarly, all methods declared in an interface are implicitly public and abstract. The use of these modifiers on a method declaration in an interface is discouraged as a matter of style.

This code defines a new interface named Collection that contains one constant value and three method declarations:

interface Collection {
    int MAXIMUM = 500;

    void add(Object obj);
    void delete(Object obj);
    Object find(Object obj);
    int currentCount();
}
The Collection interface can be implemented by classes that represent collections of other objects such as stacks, vectors, linked lists and so on.

Notice that each method declaration is followed by a semicolon (;) because an interface does not provide implementations for the methods declared within it.


Note: Previous releases of the Java environment let you use the abstract modifier on interface declarations and on method declarations within interfaces. However this is unnecessary as interfaces and their methods are implicitly abstract; none of the methods declared within an interface have implementations. You should no longer be using abstract in your interface declarations or in your method declarations within interfaces.


Previous | Next | Trail Map | Writing Java Programs | Objects, Classes, and Interfaces