Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Run time polymorphism Concept of OOPS [duplicate]

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.

like image 839
Aamir Avatar asked Nov 23 '22 07:11

Aamir


1 Answers

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 abstract member method 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 a method with the same signature, same return type, and a compatible throws clause 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.

like image 54
Sotirios Delimanolis Avatar answered Dec 15 '22 03:12

Sotirios Delimanolis