Java Reference
In-Depth Information
10.6.1 An
Actor
interface
Code 10.9 shows
Actor
defined as an interface type.
Code 10.9
The
Actor
interface
/**
* The interface to be extended by any class wishing
* to participate in the simulation.
*/
public
interface
Actor
{
/**
* Perform the actor's regular behavior.
* @param newActors A list for receiving newly created actors.
*/
void
act(List<Actor> newActors);
/**
* Is the actor still active?
* @return true if still active, false if not.
*/
boolean
isActive();
}
Java interfaces have a number of significant features:
Concepts:
A Java
interface
is a specification of
a type (in the form
of a type name and
a set of methods)
that does not
define any imple-
mentation for the
methods.
■
The keyword
interface
is used instead of
class
in the header of the declaration.
■
All methods in an interface are abstract; no method bodies are permitted. The
abstract
keyword is not needed, therefore.
■
Interfaces do not contain any constructors.
■
All method headers in an interface have public visibility, so the
public
keyword is not needed.
■
Only
public
constant class fields (
static
and
final
) are allowed in an interface. The
public
,
static
, and
final
keywords may be omitted, therefore.
A class can inherit from an interface in a similar way to that for inheriting from a class.
However, Java uses a different keyword—
implements
—for inheriting interfaces.
A class is said to
implement
an interface if it includes an
implements clause
in its class header.
For instance:
public class Fox extends Animal implements Drawable
{
//
Body of class omitted.
}
As in this case, if a class both extends a class and implements an interface, then the extends
clause must be written first in the class header.
Search WWH ::
Custom Search