howly howly - 2 months ago 11
Java Question

How does @Override annotation work in java?

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?

Answer Source

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.