We define an annotation as an interface as below
@interface annot_name {
}
and we know that all annotations extends interface java.lang.annotation.Annotation by default.
When I checked the java library for Annotation interface I found that it was overridding many methods of Object class like hashCode() etc.
If Annotation is an interface then how could it extend an Object class and override its methods? Interfaces can only extends other interfaces and not classes.
So my question is if Annotation is an interface then how could it extends an Object class and override its methods
Not exactly. The Java Language Specification §9.2 says
If an interface has no direct superinterfaces, then the interface implicitly declares a
public abstractmember methodmwith signatures, return typer, andthrowsclausetcorresponding to eachpublicinstance methodmwith signatures, return typer, andthrowsclausetdeclared inObject, unless a method with the same signature, same return type, and a compatiblethrowsclause is explicitly declared by the interface.
So any interface gets those methods.
For Annotation, the designers just chose to re-declare them in the source code so they could document their behavior.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With