4

I understand that @Override annotation in java simply tells the compiler to check and see if it's actually overriding a method in the super class during compile time.

From the article that I read: https://dzone.com/articles/how-annotations-work-java.

@Override annotation definition is simply the following:

@Target(ElementType.METHOD)
@Retention(RetentionPolicy.SOURCE)
public @interface Override {
}

This annotation definition has no logic on telling the compiler to look for the same method signature in the super class.

Is the logic to check it's super class for the same method signature define in the java compiler?

howly
  • 417
  • 1
  • 3
  • 12
  • Yes, it is. You will receive a compile error if a method annotated with @Override does not actually override anything. That's not part of the annotation's declaration though. – f1sh Aug 17 '17 at 16:00
  • Well the compiler will crawl annotations so it makes sense that the logic to check for method signatures ultimately will pertain to the compiler. Also because the retention policy states that the annotation will subsequently be discarded. – Mena Aug 17 '17 at 16:02

1 Answers1

4

It's just a hint to the compiler. By annotating with @Override, you tell the compiler that you have overriden a method from a superclass. The compiler then checks for you if you have indeed overridden this method and gives you a warning if you haven't. That is especially helpful to ensure correct spelling. The compiler actually implements this logic.

Tobias Geiselmann
  • 2,139
  • 2
  • 23
  • 36