Start of Tutorial > Start of Trail > Start of Lesson | Search |
The following figure shows that an interface definition has two components: the interface declaration and the interface body. The interface declaration declares various attributes about the interface such as its name and whether it extends another interface. The interface body contains the constant and method declarations within that interface.
The declaration for theSleeper
interface uses the two required elements of an interface declaration--theinterface
keyword and the name of the interface--plus thepublic
access modifier :An interface declaration can have one other component: thepublic interface Sleeper { . . . }public
access specifier and a list of superinterfaces. The full interface declaration looks like this figure:The
This figure has been reduced to fit on the page.
Click the image to view it at its natural size.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 be accessible only to classes that are defined in the same package as the interface.An interface can extend other interfaces just as a class can extend or subclass another class. However, while 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 of the interfaces extended by the new interface. An interface inherits all constants and methods from its superinterfaces, unless the interface hides a constant with another of the same name or redeclares a method with a new method declaration.
The interface body contains method declarations for all of the methods included in the interface. Here's the body of theSleeper
interface:The method declaration forpublic interface Sleeper { public void wakeUp(); public long ONE_SECOND = 1000; }wakeUp
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
. The use of these modifiers on a method declaration in an interface is discouraged as a matter of style.An interface can contain constant declarations in addition to method declarations. The
Sleeper
interface declares two constants that are useful arguments toletMeSleepFor
. All constant values defined in an interface are implicitlypublic
,static
, andfinal
. The use of these modifiers on a constant declaration in an interface is discouraged as a matter of style.Any class can use an interface's constants from the name of the interface, like this:
Classes that implement an interface can treat the constants as though they were inherited. This is whySleeper.ONE_SECONDGUIClock
can useONE_SECOND
directly when callingletMeSleepFor
:Member declarations in an interface disallow the use of some declaration modifiers; you may not usepublic class GUIClock extends Applet implements Sleeper { . . . public void wakeUp() { repaint(); clock.letMeSleepFor(this, ONE_SECOND); } }transient
,volatile
, orsynchronized
in a member declaration in an interface. Also, you may not 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, since interfaces and their methods are implicitly abstract. You should no longer be usingabstract
in your interface declarations or in your method declarations within interfaces.
Start of Tutorial > Start of Trail > Start of Lesson | Search |