JLS 10

From JVMLangSummit
Revision as of 15:38, 5 October 2010 by Abuckley (talk | contribs) (New page: = Interfaces = An interface declaration introduces a new reference type whose members are classes, interfaces, constants and abstract methods. This type has no implementation, but otherwi...)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigationJump to search


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.

We distinguish between two kinds of interfaces - normal interfaces and annotation types.

This chapter discusses the common semantics of all interfaces—normal interfaces and annotation types (§9.6), 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.

Interface Declarations

An interface declaration specifies a new named reference type. There are two kinds of interface declarations - normal interface declarations and annotation type declarations:


Annotation types are described further in §9.6.

    InterfaceModifiersopt interface Identifier TypeParametersopt ExtendsInterfacesopt InterfaceBody

The Identifier in an interface declaration specifies the name of the interface.A compile-time error occurs if the Identifier naming an interface appears as the name of any other top level class or interface in the same package. A compile-time error also occurs if the Identifier naming an top level 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.

A compile-time error occurs if an interface has the same simple name as any of its enclosing classes or interfaces.

This restriction stems from the Typename.this syntax, which should unambiguously identify a lexically enclosing instance. So, does this rule also pertain to nested interfaces (these are always static, so the rationale is irrelevant.)