• Post Reply Bookmark Topic Watch Topic
  • New Topic

The purpose of Override annotation when overriding works without it too  RSS feed

 
Monica Shiralkar
Ranch Hand
Posts: 922
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What is the purpose of Override annotation when overriding works without it too?

thanks
 
Tim Harris
Ranch Hand
Posts: 57
3
Chrome Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
From the Oracle Documentation:

When overriding a method, you might want to use the @Override annotation that instructs the compiler that you intend to override a method in the superclass.


In other words, to ensure that the compiler knows which method should be called.
 
Monica Shiralkar
Ranch Hand
Posts: 922
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
to ensure that the compiler knows which method should be called.


Which method is to be called is to be decided by the Signature. What does the annotation do?


thanks
 
Tim Harris
Ranch Hand
Posts: 57
3
Chrome Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's metadata:
Annotations, a form of metadata, provide data about a program that is not part of the program itself. Annotations have no direct effect on the operation of the code they annotate.


Also because I like documentation, here's some more:
While it is not required to use this annotation when overriding a method, it helps to prevent errors. If a method marked with @Override fails to correctly override a method in one of its superclasses, the compiler generates an error.


So, to answer your original question: It's a way to help keep things ironclad.
 
Monica Shiralkar
Ranch Hand
Posts: 922
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
but the compiler would have anyway given error had it been an incorrect overriding attempt
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Monica Shiralkar wrote:but the compiler would have anyway given error had it been an incorrect overriding attempt


Incorrect. If you misspelled the method, used the wrong case, or used the wrong signature, the compiler would assume that it is overloading* (instead of overriding). With the annotation, the compiler will give a compile error if it is not overriding.

Henry


* Alternatively, it may also treat it as a completely different method.
 
Monica Shiralkar
Ranch Hand
Posts: 922
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Understood it.

Thanks all
 
Rob Spoor
Sheriff
Posts: 21135
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A good example are equals and hashCode. Too often do people make the mistake to either overload equals or mistype hashCode:
Apply @Override to both methods and you'll immediately be informed that you've made a mistake.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!