Friday, April 13, 2007

INTERFACES

INTERFACES

An interface declaration introduces new reference types whose members are classes, interfaces, constants and abstract methods. Being without implementation, unrelated classes can implement interfaces by providing a definition for its abstract methods.

There are two kinds of interfaces, normal interfaces and annotation types, and they both can either be top level interfaces or nested interfaces (i.e with their declaration in the body of another class or interface).

Interface Declarations
An interface declaration specifies a new named reference type. There are two kinds of interface declarations:
NormalInterfaceDeclaration
AnnotationTypeDeclaration
A normal interface declaration is notated thus:
InterfaceModifiersopt interface Identifier TypeParameteropt ExtendsInterfacesopt InterfaceBody

Annotation type declarations will be broached later.

The identifier in an interface declaration denotes the name of the interface which name should not be the same as an enclosing class or interface otherwise, a compile time error will result.

Interface Modifiers
The interface declaration may include interface modifiers which may be one of:
public protected private abstract static strictfp

not all modifiers are applicable to all interfaces. Protected and private are applicable only to member interfaces within a directly enclosing class declaration; static applies only to member interfaces; the same modifier should not appear more than once in the same interface declaration.

If an annotation a for an interface declaration corresponds to an annotation type T, and T has a (meta-)annotation m such that m corresponds to annotation.Target, then m must have an element whose value is annotation.ElementType.TYPE, otherwise a compile time error results.

Note that every interface is implicitly abstract, but the use of this modifier is obsolete; a strictfp interface declaration is to make all double and float expressions in the interface be explicitly FP-strict, including nested types declared in the interface.

Generic interfaces and type parameters
Generic interfaces are interfaces which declare one or more type variables which are known as the type parameters of the interface and delimited by angle brackets, ‘<’ and ‘>’. A generic interface declaration defines a set of types, one for each possible invocation of the type parameter section. All type parameters share the same interface at runtime.

The scope of an interface’s type parameter is the entire declaration of the interface including the type parameter section itself. Therefore, type parameters can appear as part of their own bounds, or as bounds of other type parameters declared in the same section.

Never refer to the type parameter of an interface anywhere in a member field or member type of an interface.

Superinterfaces and subinterfaces:
If the 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, called the direct superinterfaces of the interface being declared.

If a class implements the declared interface, it is also considered to implement all the interfaces this interface extends.

Notation: extends InterfaceType(s)

Note that interface types can be generic and must be accessible.

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:
1. I directly depends on T
2. I directly depends on a class C that depends on T
3. I directly depends on an interface J that depends on T (using this definition recursively.)
An interface cannot directly depend on itself.

While every class is an extension of the Object class, there is no interface such that all interfaces implicitly extends it.

The suerinterface relationship is a transitive closure of the direct superinterface relationship. An interface K is a superinterface of interface I if either of the following is true:
1. K is a direct superinterface of I
2. an interface J exists such that K is an interface of J, and J is a superinterface of I, applying this definition recursively.
Where K is a superinterface of J then J is said to be a subinterface of K.

Interface Body and member declarations: the body of an interface may declare members of the interface where the members are either:
1. a constant declaration
2. an abstract method declaration
3. a class declaration
4. an interface declaration.
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.

Access to interface member names: all interface members are implicitly public and access to them is allowed if the interface is also declared public or protected.

No comments: