Trail: Learning the Java Language
|
Lesson: More Features of the Java Language
|
|
Defining an Interface
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 Interface Declaration
The declaration for the Sleeper interface uses the
two required elements of an interface declaration--the interface
keyword and the name of the interface--plus the public
access modifier :
public interface Sleeper {
. . .
}
An interface declaration can have one other component:
the public access specifier
and a list of superinterfaces.
The full interface declaration looks like this figure:
 This figure has been reduced to fit on the page. Click the image to view it at its natural size.
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 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-delimited 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
The interface body contains method declarations for all of the methods
included in the interface. Here's the body of the Sleeper
interface:
public interface Sleeper {
public void wakeUp();
public long ONE_SECOND = 1000;
public long ONE_MINUTE = 60000;
}
The method declaration for 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 implicitly
public and abstract.
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 to letMeSleepFor.
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.
Any class can use an interface's constants from the name of the
interface, like this:
Sleeper.ONE_SECOND
Classes that implement an interface can treat the constants as though
they were inherited. This is why GUIClock
can use ONE_MINUTE directly
when calling letMeSleepFor:
public class GUIClock extends Applet implements Sleeper {
. . .
public void wakeUp() {
repaint();
clock.letMeSleepFor(this, ONE_MINUTE);
}
}
Member declarations in an interface disallow the use of some
declaration modifiers; 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.
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, since interfaces and their
methods are implicitly abstract. You should no longer be using
abstract
in your interface declarations or in your method declarations within
interfaces.
|