• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

@Override

 
Dan Bromberg
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I just read a book where @Override was used in a subclass.
I don't recall ever seeing that when I studied Java a few years ago - is there an additional benefit or a new requirement to use it?
Dan
 
Luke Leber
Ranch Hand
Posts: 69
2
C++ Linux Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's an annotation that serves as a piece of "free" meta-information that may be used by a compiler to assist in complex static analysis. To me, it also serves as a form of self-documentation.

You are in no way required to use it to produce well-formed code, however.
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It also has the benefit of catching typos in the signature of the overriding method.
If you added a tostring (with lowercase 's') method to your class without the annotation, the code would compile but you would then have to spend time working out why your toString method wasn't being called when you ran your code.
With the annotation included, you would get a compilation error and so know about the mistake much earlier.

And despite the forum you posted this in, this feature has been included since Java 1.5
 
Ulf Dittmer
Rancher
Posts: 42968
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Annotations were introduced in Java 5, so they have been around for quite a while. "Override" is one of the built-in ones that comes with the JRE, and -due to what Joanne said- I strongly recommend to get into the habit of using it.

Here's more information: http://docs.oracle.com/javase/tutorial/java/annotations/index.html
 
Dan Bromberg
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Luke Leber wrote:It's an annotation that serves as a piece of "free" meta-information that may be used by a compiler to assist in complex static analysis. To me, it also serves as a form of self-documentation.

You are in no way required to use it to produce well-formed code, however.


Luke,
Thanks - much appreciated!
Dan
 
Dan Bromberg
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ulf Dittmer wrote:Annotations were introduced in Java 5, so they have been around for quite a while. "Override" is one of the built-in ones that comes with the JRE, and -due to what Joanne said- I strongly recommend to get into the habit of using it.

Here's more information: http://docs.oracle.com/javase/tutorial/java/annotations/index.html


Ulf,
Thanks - much appreciated!
Dan
 
Dan Bromberg
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Joanne Neal wrote:It also has the benefit of catching typos in the signature of the overriding method.
If you added a tostring (with lowercase 's') method to your class without the annotation, the code would compile but you would then have to spend time working out why your toString method wasn't being called when you ran your code.
With the annotation included, you would get a compilation error and so know about the mistake much earlier.

And despite the forum you posted this in, this feature has been included since Java 1.5


Joanne,
Thanks - much appreciated!
Dan
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic