An interface is syntactically much like an abstract class, but with no constructors, method bodies, or instance variables. There is no trace of an implementation in an interface. An interface can be used to define a restricted view of a group of objects, or to specify a minimal set of features a group of objects is expected to have for some particular purpose. A principal use of an interface is to define precisely the behavior a client requires of a server.
The syntactic structure of an interface is similar to that of a class, with the keyword interface
used in place of the keyword class
:
interface Portable { // member declarations }
An interface cannot be instantiated. Members can only be abstract methods and named constants. A method is implicitly abstract and public. A constant is implicitly static and final. By convention, keywords abstract
and public
are not used when specifying a method, and keywords static
and final
are not used when defining a constant.
public interface Switchable { int state (); void flip (); int ON = 1; int OFF = 0; }
An interface can extend any number of interfaces:
public interface Wearable extends Portable, Buyable { ... }
An interface cannot extend a class.
Subtyping and inheritance are as with classes. In the above example, the interface Wearable
inherits members from both Portable
and Buyable
. The type
reference-to-Wearable
is a subtype of both
reference-to-Portable
and
reference-to-Buyable
.
An interface may inherit several constants with the same name. Such a constant must be qualified if used in the subinterface. For example, suppose both Portable
and Buyable
contained a definition of a constant IN_USE
:
public interface Portable { ... int IN_USE = 1; } public interface Buyable { ... int IN_USE = 2; }
References to these constants in Wearable
requires qualification:
public class Wearable extends Portable, Buyable { ... int IN_USE = Portable.IN_USE + Buyable.IN_USE; }
An interface may inherit several methods with the same name, as long as they differ in number and/or types of parameters. If two superinterfaces both define a method with the same name and parameter types, then the return types of the methods must be identical. For example, if Portable
declares a method
void pickUp (int weight);
and Buyable
declares a method
void pickUp (double cost);
then Wearable
inherits both. If Portable
declares
void pickUp (int weight);
and Buyable
declares
void pickUp (int cost);
then Wearable
inherits a method named pickUp
with a single int
parameter. But if Portable
declares
void pickUp (int weight);
and Buyable
declares
boolean pickUp (int cost);
then Wearable
cannot extend both Portable
and Buyable
.
A class can implement any number of interfaces, in which case it inherits their members.
public class Room implements Cloneable, MazeFeature
A concrete class that implements an interface must override inherited abstract methods, providing implementations. Subtyping and inheritance are as explained above and in the section on classes.