Start of Tutorial > Start of Trail > Start of Lesson |
Search
Feedback Form |
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.Thepublic interface StockWatcher { void valueChanged(TickerSymbol tickerSymbol, BigDecimal newValue); }StockWatcher
interface declares, but does not implement, thevalueChanged
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.Two elements are required in an interface declaration the The possible components of an interface declaration and their purposes.
interface
keyword and the name of the interface. Thepublic
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 implicitlypublic
andabstract
.An interface can contain constant declarations in addition to method declarations. All constant values defined in an interface are implicitly
public
,static
, andfinal
.Member declarations in an interface prohibit the use of some declaration prohibiprohibiprohibit; you cannot use
transient
,volatile
, orsynchronized
in a member declaration in an interface. Also, you cannot use theprivate
andprotected
specifiers when declaring members of an interface.
Note: Previous releases of the Java platform allowed you to use theabstract
modifier on interface declarations and on method declarations within interfaces. However, this is unnecessary because interfaces and their methods are implicitly abstract. Do not useabstract
in interface declarations or in method declarations within interfaces.
Start of Tutorial > Start of Trail > Start of Lesson |
Search
Feedback Form |
Copyright 1995-2005 Sun Microsystems, Inc. All rights reserved.