An interface declaration specifies a new reference type:
InterfaceDeclaration:
InterfaceModifiersoptinterface
Identifier
ExtendsInterfacesoptInterfaceBody
A compile-time error occurs if the Identifier naming an interface appears as the name of any other class or interface in the same package. A compile-time error also occurs if the Identifier naming an interface appears as the name by which a class or interface is to be known via a single-type-import declaration in the compilation unit containing the interface declaration. In the example:
class Point { int x, y; }
interface Point { void move(int dx, int dy); }
a compile-time error occurs because a class
and an interface
in the same package cannot have the same name.
The Identifier specifies the name of the interface and has as its scope the entire package in which it is declared. This is the same scoping rule as for class type names.
An interface declaration may be preceded by interface modifiers:
InterfaceModifiers:
InterfaceModifier
InterfaceModifiersInterfaceModifier InterfaceModifier: one of
public abstract
A compile-time error occurs if the same modifier appears more than once in an interface declaration.
Every interface is implicitly abstract
. This modifier is obsolete and should not be used in new Java programs.
If an extends
clause is provided, then the interface being declared extends each of the other named interfaces and therefore inherits the methods and constants of each of the other named interfaces. These other named interfaces are the direct superinterfaces of the interface being declared. Any class that implements
the declared interface is also considered to implement all the interfaces that this interface extends
and that are accessible to the class.
ExtendsInterfaces:
extends
InterfaceType
ExtendsInterfaces,
InterfaceType
The following is repeated to make the presentation here clearer:
InterfaceType:
TypeName
Each InterfaceType in the extends
clause of an interface declaration must name an accessible interface type; otherwise a compile-time error occurs.
A compile-time error occurs if there is a circularity such that an interface directly or indirectly extends itself.
There is no analogue of the class Object
for interfaces; that is, while every class is an extension of class Object
, there is no single interface of which all interfaces are extensions.
The superinterface relationship is the transitive closure of the direct superinterface relationship. An interface K is a superinterface of interface I if either of the following is true:
Interface I is said to be a subinterface of interface K whenever K is a superinterface of I.
The body of an interface may declare members of the interface:
InterfaceBody:
{
InterfaceMemberDeclarationsopt}
InterfaceMemberDeclarations:
InterfaceMemberDeclaration
InterfaceMemberDeclarationsInterfaceMemberDeclaration InterfaceMemberDeclaration:
ConstantDeclaration
AbstractMethodDeclaration
The scope of the name of a member declared in an interface type is the entire body of the interface type declaration.
All interface members are implicitly public
. They are accessible outside the package where the interface is declared if the interface is also declared public
and the package containing the interface is accessible.