The JavaTM Tutorial
Previous Page Lesson Contents Next Page Start of Tutorial > Start of Trail > Start of Lesson Search
Feedback Form

Trail: Learning the Java Language
Lesson: Interfaces and Packages

Defining an Interface

The following figure shows that an interface definition has two components: the interface declaration and the interface body. The interface declaration defines various attributes of the interface, such as its name and whether it extends other interfaces. The interface body contains the constant and the method declarations for that interface.

The StockWatcher interface and the structure of an interface definition.

The StockWatcher interface and the structure of an interface definition.

public interface StockWatcher {
    void valueChanged(TickerSymbol tickerSymbol,
                      BigDecimal newValue);
}
The StockWatcher interface declares, but does not implement, the valueChanged method. Classes that implement this interface provide the implementation for that method.

The Interface Declaration

The following figure shows all possible components of an interface declaration.

The possible components of an interface declaration and their purposes.

The possible components of an interface declaration and their purposes.

Two elements are required in an interface declaration — the interface keyword and the name of the interface. The public access specifier indicates that the interface can be used by any class in any package. If you do not specify that the interface is public, your interface will be accessible only to classes defined in the same package as the interface.

An interface declaration can have one other component: a list of superinterfaces. An interface can extend other interfaces, just as a class can extend or subclass another class. However, whereas a class can extend only one other class, an interface can extend any number of interfaces. The list of superinterfaces is a comma-separated list of all the interfaces extended by the new interface.

The Interface Body

The interface body contains method declarations for all the methods included in the interface. A method declaration within an interface is followed by a semicolon (;) because an interface does not provide implementations for the methods declared within it. All methods declared in an interface are implicitly public and abstract.

An interface can contain constant declarations in addition to method declarations. All constant values defined in an interface are implicitly public, static, and final.

Member declarations in an interface prohibit the use of some declaration prohibiprohibiprohibit; you cannot use transient, volatile, or synchronized in a member declaration in an interface. Also, you cannot use the private and protected specifiers when declaring members of an interface.


Note:  Previous releases of the Java platform allowed you to use the abstract modifier on interface declarations and on method declarations within interfaces. However, this is unnecessary because interfaces and their methods are implicitly abstract. Do not use abstract in interface declarations or in method declarations within interfaces.

Previous Page Lesson Contents Next Page Start of Tutorial > Start of Trail > Start of Lesson Search
Feedback Form

Copyright 1995-2005 Sun Microsystems, Inc. All rights reserved.