• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Question about @Override annotation

 
Ranch Hand
Posts: 85
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I am reading the following source code:

class secondCountDownRunner implements Runnable{
@Override
public void run() {
// ..implementation of the run method
}
}

And I have a question about @Override annotation. What does it do? Why i need to put it in my code? Isn't compiler know i am implementing the run()
of the Runnable interface?

Thank you.
 
Ranch Hand
Posts: 15304
6
Mac OS X IntelliJ IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It's really there as meta-data for you as the developer so when you see it you know that the marked method is actually overriding a method from the parent class. IDE's use it as well. Other than that, it does nothing. Removing the annotation has no effect as far as I know on the code.
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It is really intended for overriding already implemented methods from a superclass; it is supposed to catch those annoying little spelling errors that are so hard to find when the method doesn't work. It is permitted in Java6 for methods implemented from an interface, but is redundant in that instance.
 
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Don't know about other IDE's but in Eclipse:
If the overridden method is later renamed or deleted, it causes a compile error in the overriding method. A useful safety net.
You can also change the options to make it generate a warning if @Override is not specified where it is applicable, which enforces using the safety net.
 
Campbell Ritchie
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That is a nice feature about Eclipse; you can add all sorts of warnings if you use slightly un-stylish code, to maintain the code quality. I suspect other IDEs have similar warning options.
 
Attractive, successful people love this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic