Which of the following Declarations Are Legal within the Body of an Interface
One consequence of an annotation type not being able to explicitly declare a superclass or superinterface is that a subclass or subinterface of an annotation type is never itself an annotation type. Similarly, java.lang.annotation.annotation itself is not an annotation type. For example, annotation types use the same namespace as normal class and interface types. And annotation type declarations are allowed wherever interface declarations are legal and have the same scope and accessibility. Second, interfaces do not inherit from Object, but implicitly declare many of the same methods as Object (§9.2). There is therefore no common ancestor for the toString declared in Object and the toString declared in an interface. If the two candidates were candidates for inheritance by a class, they would at best come into conflict. To work around this problem, the class and interface inheritance trees would have to be heavily mixed. The implicitly declared annotation is called container annotation, and multiple T-type annotations that appear in context are called base annotations. The elements of the value element (array type) of the container annotation are all basic annotations in the order from left to right in which they were displayed in context. An interface declaration specifies a new named reference type.
There are two types of interface declarations – normal interface declarations and annotation type declarations (§9.6). If an abstract method and a standard method with corresponding signatures are inherited from a subinterface, we generate an error. In this case, it would be possible to give priority to one or the other – perhaps we would assume that the standard method provides a meaningful implementation for the abstract method. However, this is risky because, aside from the random name and signature, we have no reason to believe that the default method behaves consistently with the abstract method contract – the default method may not even have existed when the subinterface was originally developed. In this situation, it is safer to ask the user to actively confirm that the default implementation is appropriate (via a priority declaration). An instance method mI declared in or inherited from interface I replaces another instance method mJ declared in interface J from I if all of the following conditions are true: The function type of a functional interface is defined non-deterministically: while the signatures in M are “equal”, they can be syntactically different (for example, HashMap.Entry and Map.Entry); The return type can be a subtype of any other return type, but there can be other return types that are also subtypes (for example, List and List); and the order of the types triggered is not specified. These distinctions are subtle, but they can sometimes be important. However, function types are not used in the Java language in a way that implies non-determinism. Note that the return type and launch clause of a “most specific method” are also defined non-deterministically if there are multiple abstract methods (§15.12.2.5). An ElementValueArrayInitializer is similar to a normal array initializer (§10.6), except that an ElementValueArrayInitializer can contain syntactic annotations and nested expressions and initializers. However, nested initializers are not allowed semantically in an ElementValueArrayInitializer because they never match array-typed elements in annotation type declarations (nested array types are not allowed). It is common, but not required, to write declaration annotations before all other modifiers and to enter annotations immediately before the type to which they apply.
For an interface I, let M be the set of abstract methods that are members of I that do not have the same signature as an Object class public instance method (§4.3.2). Then I is a functional interface if there exists a method m in M for which both of the following conditions are true: a formal parameter or exception declaration, but T is not applicable to formal or exception parameter declarations or type contexts. Then, the throws clause of the function type contains any type E that satisfies the following constraints: An interface does not inherit private or static methods from its superinterfaces. This is a compilation error if an interface method declaration that contains the keyword summary also contains the strictfp keyword. If this code were legal, several containment levels would be required: first, Foo annotations would be included by an implicitly declared FooContainer container annotation, and then this annotation and the explicitly declared FooContainer annotation would be included in another implicitly declared annotation. According to the developers of the Java programming language, this complexity is not desirable. Any other approach of treating Foo annotations as if they appeared next to @Foo(2) in the explicit annotation @FooContainer is not desirable because it could change the way reflective programs interpret the @FooContainer annotation. If Foo is applicable to field and method declarations, FooContainer cannot legitimately serve as Foo`s annotation type when FooContainer is applicable to field and parameter declarations. Although it is possible to take the intersection of program elements and make Foo reproducible only for field declarations, the presence of additional program elements for FooContainer indicates that FooContainer was not designed as an annotation type for Foo.
It would therefore be dangerous for Foo to rely on it. It is used in an import declaration that imports the normally obsolete type or member. An annotation is a marker that binds information to a program construct, but has no effect at run time. An annotation refers to a call specific to an annotation type (§9.6) and usually provides values for elements of that type. Programs can use interfaces to eliminate the need for related classes to share a common abstract superclass or to add methods to Object. An important property of this syntax is that in two declarations that differ only in the number of array levels, the annotations to the left of the type refer to the same type. For example, @C applies to the int type in all of the following statements: Finally, the following examples show the same rules as above, but using generic methods: It is used as part of an export or opening policy (§7.7.2). Formal and exception parameter declarations (§8.4.1, §9.4, §14.20) It is possible for an interface to inherit from several methods with equivalent signatures (§8.4.2).
Any class that implements the declared interface is also assumed to implement all interfaces extended by that interface. If an interface does not have super direct interfaces, the interface implicitly declares a public abstract member method m with signature s, return type r, and throws clause t according to each public instance method m with signature s, return type r, and throws clause t into the object (§4.3.2), unless it is an abstract method with the same signature, The same type of return and a compatible launch clause are explicitly declared by the interface. A function type can be generic because the abstract method of a functional interface can be generic. For example, in the following interface hierarchy: If a single field is inherited multiple times from the same interface, for example because that interface and one of the direct superinterfaces of that interface extend the interface that declares the field, then only one element results. This situation itself does not cause a compilation error. These other named interfaces are the super direct interfaces of the declared interface. Fourth, Object methods are so fundamental that it seems dangerous to allow a superinterface to silently add a default method that changes its behavior. An interface can be declared a direct extension of one or more other interfaces, which means that it inherits all member types, instance methods, and constants from the interfaces it extends, except for elements that it can replace or hide. modifier statics refer only to member interfaces (§8.5.1, §9.5), not to higher-level interfaces (§7.6). This is a compilation error when a method declared in an annotation type has a signature that matches that of a public or protected method declared in the Object class or in the java.lang.annotation.Annotation interface.
If a method is declared with a return type (§8.4.5), a compilation error occurs when the body of the method can complete normally (§14.1).