JLS 10

From JVMLangSummit
Revision as of 16:01, 5 October 2010 by Abuckley (talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigationJump to search

Interfaces

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:

InterfaceDeclaration:
    NormalInterfaceDeclaration
    AnnotationTypeDeclaration

Annotation types are described further in §9.6.

NormalInterfaceDeclaration:
    InterfaceModifiersopt interface Identifier TypeParametersopt ExtendsInterfacesopt InterfaceBody

The Identifier in an interface declaration specifies the name of the interface.

kind:error 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.

[[kind:example 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.)

Scope of an Interface Type 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; see §8.1.1 for an example involving classes. Redundant with chapter 6. Also, only applies to top level interfaces.

Interface Modifiers

An interface declaration may include interface modifiers:

InterfaceModifiers:
    InterfaceModifier
    InterfaceModifiers InterfaceModifier
InterfaceModifier: one of
    Annotation public protected private abstract static strictfp

The 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. A compile-time error occurs if an interface declaration has both the strictfp and widefp modifiers.

If an annotation a on an interface declaration corresponds to an annotation type T, and T has a (meta-)annotation m that corresponds to annotation.Target, then m must have an element whose value is annotation.ElementType.TYPE, or a compile-time error occurs. Annotation modifiers are described further in §9.7.

abstract Interfaces

Every interface is implicitly abstract. This modifier is obsolete and should not be used in new programs.

strictfp Interfaces

The effect of the strictfp 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.