Contents | Prev | Next | Index | Java Language Specification Second Edition |
CHAPTER 9
An interface declaration introduces a new reference type whose members are classes, interfaces, constants and abstract methods. This type has no implementation, but otherwise unrelated classes can implement it by providing implementations for its abstract methods.
A nested interface is any interface whose declaration occurs within the body of another class or interface. A top-level interface is an interface that is not a nested interface.
This chapter discusses the common semantics of all interfaces-top-level (§7.6) and nested (§8.5, §9.5). Details that are specific to particular kinds of interfaces are discussed in the sections dedicated to these constructs.
Programs can use interfaces to make it unnecessary for related classes to share a common abstract superclass or to add methods to Object
.
An interface may be declared to be a direct extension of one or more other interfaces, meaning that it implicitly specifies all the member types, abstract methods and constants of the interfaces it extends, except for any member types and constants that it may hide.
A class may be declared to directly implement one or more interfaces, meaning that any instance of the class implements all the abstract methods specified by the interface or interfaces. A class necessarily implements all the interfaces that its direct superclasses and direct superinterfaces do. This (multiple) interface inheritance allows objects to support (multiple) common behaviors without sharing any implementation.
A variable whose declared type is an interface type may have as its value a reference to any instance of a class which implements the specified interface. It is not sufficient that the class happen to implement all the abstract methods of the interface; the class or one of its superclasses must actually be declared to implement the interface, or else the class is not considered to implement the interface.
InterfaceDeclaration: InterfaceModifiersopt interface Identifier ExtendsInterfacesopt InterfaceBodyThe Identifier in an interface declaration specifies the name of the interface. A compile-time error occurs if an interface has the same simple name as any of its enclosing classes or interfaces.
InterfaceModifiers: InterfaceModifier InterfaceModifiers InterfaceModifier InterfaceModifier: one of public protected private abstract static strictfpThe access modifier public is discussed in §6.6. Not all modifiers are applicable to all kinds of interface declarations. The access modifiers protected and private pertain only to member interfaces within a directly enclosing class declaration (§8.5) and are discussed in §8.5.1. The access modifier static pertains only to member interfaces (§8.5, §9.5). A compile-time error occurs if the same modifier appears more than once in an interface declaration.
abstract
Interfacesabstract
. This modifier is obsolete and should not be used in new programs.strictfp
Interfacesstrictfp
modifier is to make all float
or double
expressions within the interface declaration be explicitly FP-strict (§15.4).
This implies that all nested types declared in the interface are implicitly strictfp
.
extends
clause is provided, then the interface being declared extends each of the other named interfaces and therefore inherits the member types, 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
.
ExtendsInterfaces: extends InterfaceType ExtendsInterfaces , InterfaceTypeThe following is repeated from §4.2 to make the presentation here clearer:
InterfaceType: TypeNameEach InterfaceType in the
extends
clause of an interface declaration must name an accessible interface type; otherwise a compile-time error occurs.
An interface I directly depends on a type T if T is mentioned in the extends
clause of I either as a superinterface or as a qualifier within a superinterface name. An interface I depends on a reference type T if any of the following conditions hold:
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:
InterfaceBody:
{ InterfaceMemberDeclarationsopt }
InterfaceMemberDeclarations:
InterfaceMemberDeclaration
InterfaceMemberDeclarations InterfaceMemberDeclaration
InterfaceMemberDeclaration:
ConstantDeclaration
AbstractMethodDeclaration
ClassDeclaration
InterfaceDeclaration
;
The scope of the declaration of a member m declared in or inherited by an interface type I is the entire body of I, including any nested type declarations.public
. They are accessible outside the package where the interface is declared if the interface is also declared public
or protected
, in accordance with the rules of §6.6.
throws
clause t corresponding to each public instance method m with signature s, return type r, and throws
clause t declared in Object
, unless a method with the same signature, same return type, and a compatible throws
clause is explicitly declared by the interface.
throws
clause.The interface inherits, from the interfaces it extends, all members of those interfaces, except for fields, classes, and interfaces that it hides and methods that it overrides.
ConstantDeclaration: ConstantModifiersopt Type VariableDeclarators ConstantModifiers: ConstantModifierEvery field declaration in the body of an interface is implicitlyConstantModifiers ConstantModifer
ConstantModifier: one of public static final
public
, static
, and final
. It is permitted to redundantly specify any or all of these modifiers for such fields.If the interface declares a field with a certain name, then the declaration of that field is said to hide any and all accessible declarations of fields with the same name in superinterfaces of the interface.
It is a compile-time error for the body of an interface declaration to declare two fields with the same name.
It is possible for an interface to inherit more than one field with the same name (§8.3.3.3). Such a situation does not in itself cause a compile-time error. However, any attempt within the body of the interface to refer to either field by its simple name will result in a compile-time error, because such a reference is ambiguous.
There might be several paths by which the same field declaration might be inherited from an interface. In such a situation, the field is considered to be inherited only once, and it may be referred to by its simple name without ambiguity.
A compile-time error occurs if an initialization expression for an interface field contains a reference by simple name to the same field or to another field whose declaration occurs textually later in the same interface.
causes two compile-time errors, becauseinterface Test { float f = j; int j = 1; int k = k+1; }
j
is referred to in the initialization of f
before j
is declared and because the initialization of k
refers to k
itself.
One subtlety here is that, at run time, fields that are initialized with compile-time constant values are initialized first. This applies also to static
final
fields in classes (§8.3.2.1). This means, in particular, that these fields will never be observed to have their default initial values (§4.5.5), even by devious programs. See §12.4.2 and §13.4.8 for more discussion.
If the keyword this
(§15.8.3) or the keyword super
(15.11.2, 15.12) occurs in an initialization expression for a field of an interface, then unless the occurrence is within the body of an anonymous class (§15.9.5), a compile-time error occurs.
the interfaceinterface BaseColors { int RED = 1, GREEN = 2, BLUE = 4; } interface RainbowColors extends BaseColors { int YELLOW = 3, ORANGE = 5, INDIGO = 6, VIOLET = 7; } interface PrintColors extends BaseColors { int YELLOW = 8, CYAN = 16, MAGENTA = 32; } interface LotsOfColors extends RainbowColors, PrintColors { int FUCHSIA = 17, VERMILION = 43, CHARTREUSE = RED+90; }
LotsOfColors
inherits two fields named YELLOW
. This is all right as long as the interface does not contain any reference by simple name to the field YELLOW
. (Such a reference could occur within a variable initializer for a field.)
Even if interface PrintColors
were to give the value 3
to YELLOW
rather than the value 8
, a reference to field YELLOW
within interface LotsOfColors
would still be considered ambiguous.
In the example in the previous section, the fields RED
, GREEN
, and BLUE
are inherited by interface LotsOfColors
in more than one way, through interface RainbowColors
and also through interface PrintColors
, but the reference to field RED
in interface LotsOfColors
is not considered ambiguous because only one actual declaration of the field RED
is involved.
AbstractMethodDeclaration: AbstractMethodModifiersopt ResultType MethodDeclarator Throwsopt ; AbstractMethodModifiers: AbstractMethodModifier AbstractMethodModifiers AbstractMethodModifier AbstractMethodModifier: one of public abstractThe access modifier
public
is discussed in §6.6. A compile-time error occurs if the same modifier appears more than once in an abstract method declaration.
Every method declaration in the body of an interface is implicitly abstract
, so its body is always represented by a semicolon, not a block.
Every method declaration in the body of an interface is implicitly public
.
For compatibility with older versions of the Java platform, it is permitted but discouraged, as a matter of style, to redundantly specify the abstract
modifier for methods declared in interfaces.
It is permitted, but strongly discouraged as a matter of style, to redundantly specify the public
modifier for interface methods.
Note that a method declared in an interface must not be declared static
, or a compile-time error occurs, because static
methods cannot be abstract
.
Note that a method declared in an interface must not be declared strictfp
or native
or synchronized
, or a compile-time error occurs, because those keywords describe implementation properties rather than interface properties. However, a method declared in an interface may be implemented by a method that is declared strictfp
or native
or synchronized
in a class that implements the interface.
It is a compile-time error for the body of an interface to declare, explicitly or implicitly, two methods with the same signature (name, number of parameters, and types of any parameters) (§8.4.2). However, an interface may inherit several methods with the same signature (§9.4.1).
Note that a method declared in an interface must not be declared final
or a compile-time error occurs. However, a method declared in an interface may be implemented by a method that is declared final
in a class that implements the interface.
If a method declaration in an interface overrides the declaration of a method in another interface, a compile-time error occurs if the methods have different return types or if one has a return type and the other is void
. Moreover, a method declaration must not have a throws
clause that conflicts (§8.4.4) with that of any method that it overrides; otherwise, a compile-time error occurs.
Methods are overridden on a signature-by-signature basis. If, for example, an interface declares two public
methods with the same name, and a subinterface overrides one of them, the subinterface still inherits the other method.
An interface inherits from its direct superinterfaces all methods of the superinterfaces that are not overridden by a declaration in the interface.
It is possible for an interface to inherit more than one method with the same signature (§8.4.2). Such a situation does not in itself cause a compile-time error. The interface is considered to inherit all the methods. However, a compile-time error occurs if, for any two such inherited methods, either they have different return types or one has a return type and the other is void
. (The throws
clauses do not cause errors in this case.) There might be several paths by which the same method declaration is inherited from an interface. This fact causes no difficulty and never of itself results in a compile-time error.
throws
clauses of two methods with the same name but different signatures.abstract
and thus contain no implementation. About all that can be accomplished by an overriding method declaration, other than to affirm a method signature, is to restrict the exceptions that might be thrown by an implementation of the method. Here is a variation of the example shown in §8.4.3.1:
class BufferEmpty extends Exception { BufferEmpty() { super(); } BufferEmpty(String s) { super(s); } } class BufferError extends Exception { BufferError() { super(); } BufferError(String s) { super(s); } } public interface Buffer { char get() throws BufferEmpty, BufferError; } public interface InfiniteBuffer extends Buffer { char get() throws BufferError; // override }
the method nameinterface PointInterface { void move(int dx, int dy); } interface RealPointInterface extends PointInterface { void move(float dx, float dy); void move(double dx, double dy); }
move
is overloaded in interface RealPointInterface
with three different signatures, two of them declared and one inherited. Any non-abstract
class that implements interface RealPointInterface
must provide implementations of all three method signatures.static
and public
.If a member type declared with simple name C is directly enclosed within the declaration of an interface with fully qualified name N, then the member type has the fully qualified name N.C.
If the interface declares a member type with a certain name, then the declaration of that field is said to hide any and all accessible declarations of member types with the same name in superinterfaces of the interface.
An interface may inherit two or more type declarations with the same name. A compile-time error occurs on any attempt to refer to any ambiguously inherited class or interface by its simple name. If the same type declaration is inherited from an interface by multiple paths, the class or interface is considered to be inherited only once; it may be referred to by its simple name without ambiguity.
Contents | Prev | Next | Index | Java Language Specification Second Edition |