Class & interface terminology

Dan Smith daniel.smith at oracle.com
Fri May 8 18:45:33 UTC 2020


> On May 7, 2020, at 6:57 PM, forax at univ-mlv.fr wrote:
> 
> Yes, the JLS explicily says that an annotation type is an interface so it's a reified type, you can create array of them and declare member types in it (even i never seen a code like that),
> all other things are different,

So it sounds like we agree on the crucial point: whenever JLS says "interface", it includes [annotation types/interfaces].

> it has its own grammar, its own modifiers, no super-type, no overriding, no overloading, no static final/static block, etc 

The fundamental difference you're pointing out here is that interface declarations and annotation [interface] declarations are two separate things. I agree.

These two points are not in conflict: there are two different kinds of declarations, but both specify interfaces.

> Chapter 9 of the spec defines interface and Annotation are in chapter 9.6,
> but all the sub-sections of 9.6 explain why the previous sections (9.1 to 9.5) are not valid for an annotation type
> 
> 9.1  annotation types has its own grammar and its own modifier 
> 9.1.3 and 9.2: annotation types use delegation not inheritance (apart java.lang.Annotation) so all inheritance of members doesn't apply
> 9.3: constant fields: doesn't apply
> 9.4: method, overloading, overriding: doesn't apply
> 9.5: member type: partially applicable, you can put an interface inside an annotation but again 9.6 override this section with the rule about the simple name of an annotation
> 
> So to summarize, an annotation type is like an interface apart the fact that almost all the rules don't apply.

Here are the sentences I'm hoping to draw your attention to:

"We distinguish between two kinds of interfaces - normal interfaces and annotation types." (9)

"This chapter discusses the common semantics of all interfaces - normal interfaces, both top level (§7.6) and nested (§8.5, §9.5), and annotation types (§9.6). Details that are specific to particular kinds of interfaces are discussed in the sections dedicated to these constructs." (9)

"An annotation type declaration specifies a new annotation type, a special kind of interface type." (9.6)

Due to the syntax of an annotation [interface] declaration, many properties of interfaces are uninteresting/vacuous when applied to [annotation types/interfaces]. But they still apply.

So, for example, there is no 'extends' clause in an annotation [interface] declaration, but the [annotation type/interface] still has a superinterface: java.lang.annotation.Annotation. Like any interface, it has members, both declared methods (see 9.6.1: "The body of an annotation type declaration may contain method declarations, each of which defines an element of the annotation type.") and inherited methods.

But it sounds like there's no disagreement here: we both accept the model that an [annotation type/interface] is a kind of interface.

All of the below is just to reinforce this point. So that the connection is clear:

>> 2) That choice makes sense, because there are a lot of rules related to
>> interfaces that apply to [annotation types/interfaces]. I gave you one example,
>> but there are many:
>> 
>> https://docs.oracle.com/javase/specs/jls/se14/html/jls-8.html#jls-8.1.5
> 
> super-interfaces, an annotation type can not have super interfaces ??

An [annotation type/interface] can *be* a superinterface.

@interface Foo {}
abstract class Bar implements Foo {}

>> https://docs.oracle.com/javase/specs/jls/se14/html/jls-4.html#jls-4.3
> 
> it's about annotation declared on type, it's not about the annotation type, so not relevant

"There are four kinds of reference types: class types (§8.1), interface types (§9.1), type variables (§4.4), and array types (§10.1)."

Given '@inteface Foo {}', which kind of type is 'Foo'? Must be an interface type, right?

>> https://docs.oracle.com/javase/specs/jls/se14/html/jls-4.html#jls-4.7
> 
> reified type, yes, an annotation type behave like an interface

And this works because when the rule says "It refers to a non-generic class or interface type declaration", that includes [annotation types/interfaces].

> 
>> https://docs.oracle.com/javase/specs/jls/se14/html/jls-9.html#jls-9.2
> 
> see above, an annotation has no user-defined super types.

What are the members of an [annotation type/interface]? What methods can I invoke, given a variable of type 'Foo'? That's specified right here, even though the word "annotation" doesn't appear in this section.

> 
>> https://docs.oracle.com/javase/specs/jls/se14/html/jls-4.html#jls-4.10.2
> 
> subtyping, again an annotation has no user-defined super types.

It still has supertypes, though—given a variable of type 'Foo', I can assign it to some other things.

> 1/ an annotation is not an interface, an enum is not a class, trying to retrofit them the way you are trying is artificial

I've written a lot of words to try to make it clear to you that, in the JLS 14 model, these sentences are false. You said the same above—"the JLS explicily says that an annotation type is an interface". Can we agree on this point?



More information about the amber-spec-observers mailing list