• 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
  • Tim Cooke
  • paul wheaton
  • Paul Clapham
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Roland Mueller
  • Piet Souris
Bartenders:

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: 80874
506
  • 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: 80874
506
  • 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.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic