INTERFACES(2)
INTERFACES(2)
INTERFACE MEMBERS:
The members of an interface are:
1. those members declared in the interface
2. members inherited from direct superinterfaces
3. where there is no direct superinterface, then the interface implicitly declares a public abstract member m with signature s return type r and throws clause t corresponding to each public instance method m with signature s return type r and throws clause t declared in Object unless an explicit declaration of a method with the same signature, return type and compatible throws clause exists. If m is declared to be final in Object and the interface explicitly declares me, then a compile time error results.
It is a compile time error if the interface declares a method with a signature that is override-equivalent to a public method of Object, but has a different return type or incompatible throws clause.
Except for those fields, classes and interfaces that is hides and methods it overrides, an interface inherits members of an interface it extends.
FIELD (CONSTANT) DECLARATION:
Constant declaration:
ConstantModifiersopt Type VariableDeclarators;
Where constant modifiers are one of:
Annotation public static final
Note: every field declaration in the body of an interface is implicitly public static final. Redundant modifier specification is allowed.
If an annotation a on a field 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.FIELD, or a compile time error occurs.
A same name field in an interface with its superinterface hides the field of the superinterface provided they are accessible and the field exists.
Although an interface can inherit two fields with the same name, referencing this fields using their simple names will result in compile time error due to ambiguous referencing.
If inheritance of a field can be done through several paths, inheritance is considered to be just once and the field’s simple name can be used to referenced it.
Initialisation of fields in interfaces:
Every field in the body of an interface must have an initialization expression, which need not be a constant expression. When the interface is initialized, the variable initialiser is evaluated and the assignment performed exactly once.
Note:
1. initialization expression for an interface field cannot contain a reference by simple name, a. to itself; or b. to another field whose declaration occurs textually later in the same interface.
2. the keywords this or super cannot occur in the initialization expression for a field of an interface unless this occurrence is within the body of an anonymous class.
(the jls contains some straight-to-the-point examples for the above, at section §9.3)
ABSTRACT METHOD DECLARATION:
Notation:
AbstractMethodModifiersopt TypeParametersopt ResultType MethodDeclarator Throwsopt
Abstract method modifiers can be one of the following:
Annotation public abstract
Modifier appearance redundancy is not allowed.
Every method declaration in an interface body is implicitly abstract, so its body is always represented by a semicolon, ;, and not a block.
Every method declaration in the body of an interface is implicitly public.
Abstract methods cannot be static and vice versa, so member interface methods can never be static.
Annotation rules also apply to interface methods i.e if an annotation a on a method 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.METHOD, or a compile time error occurs.
These are known compile time errors:
1. for an interface to declare two methods with override-equivalent signatures, whether implicitly or explicitly. However, an interface may inherit several methods with such signatures.
2. interface methods must not be declared final although implementing classes can declare its implementation as final
3. interface methods may be generic.
Inheritance and overriding.
If an interface I has an instance method m1 and interface J has an instance method m2, m1 overrides m2 if and only if the following are true:
- I is a subinterface of J
- The signature of m1 is a subsignature of the signature of m2.
Note that overriding and hiding implies return type substitutability along with subtyping on the overriding type which can issue an unchecked warning.
A method declaration does not have a throws clause that conflicts with that of any method it overrides, otherwise, compile time error occurs.
If a type declaration T has a member method m1 and another method m2 exists, declared in T or a supertype of T, a compile time error occurs if all if all the following holds:
1. m1 and m2 have same name
2. m2 is accessible from T
3. the signature of m1 is not a subsignature of the signature of m2
4. m1 or some method m1 overrides (directly or indirectly) has the same erasure as m2 or some method m2 overrides (directly or indirectly.)
notes:
a. methods are overridden on a signature-by-signature basis.
b. All methods not overridden by an interface in the supertype are automatically inherited.
c. An interface can inherit several methods with override-equivalent signatures, however, one of the inherited methods must be return-type substitutable for any other inherited method, otherwise a compile time error occurs.
d. A method can be inherited from several paths, inheritance is considered as being only once.
Overloading: overloading of interface member methods similar to as detailed for class methods overloading.
MEMBER TYPE DECLARATIONS
Interfaces may contain member type declarations; a member type declaration in an interface is implicitly static and public.
A member type C enclosed in an interface N has a fully qualified name N.C.
Member type hiding can occur between an interfaces and its superinterface.
An interface will inherit all non-private accessible non-hidden member types of its superinterface.
An interface can inherit 2 or more type declarations with the same name, but on referencing them with their simple names, ambiguity results.
If the same type declaration is inherited with different paths by an interface, then inheritance is assumed to be but once.
No comments:
Post a Comment