Search code examples
javainterfaceannotations

Why non-final public methods of Object declared in Annotation interface?


java.lang.annotation.Annotation interface declares toString, hashCode and equals methods (non-final public methods of Object class)

I think if these methods haven't been declared in Annotation interface, nothing has been changed because these methods already defined in Object class. So we can use them even if they not declared in this interface. So Why these methods declared in this interface ? What are the necessities of them ?


Solution

  • A quick lookup on SO did not yield any significant duplicate question, so I will post my comment as an answer. If there is a duplicate, I happily remove my answer in favour of the duplicate.

    java.lang.annotatino.Annotation is somewhat special:

    The common interface extended by all annotation types. Note that an interface that manually extends this one does not define an annotation type.

    Annotations are not part of the normal object hirachy. Thus, this interface can be seen as the "root-object"1 for annotations.


    1 Annotations are not really objects, thus the quotes. There is no inheritance within the annotation types, aside from all annotations implicitly implementing the interface java.lang.annotation.Annotation.