• Post Reply Bookmark Topic Watch Topic
  • New Topic

@Override  RSS feed

 
Wolfgang Tintemann
Ranch Hand
Posts: 65
Eclipse IDE MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Often I see the word @Override ahead of function/method definitions.
In my books about Java I don't find this mentioned.

Is it necessary if one wants to overwrite a method ?
 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's not required. It's a hint to the compiler that the method is intended to override some other method. That allows the compiler to check that it actually does override some method. Otherwise, a typo in the method name or incorrect parameter types would lead to no method being overridden - and you'd be left wondering why your method doesn't get called. Also see http://download.oracle.com/javase/tutorial/java/javaOO/annotations.html

If your books don't cover it that may be a sign that they're somewhat old - it was introduced only with Java 5.
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16057
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To give you a concrete example: Consider the hashCode() method of class Object. Sometimes you want to override that method in your own classes. But it's easy to forget that the method is called hashCode(), and not hashcode() with a lower-case c. When you do this:

You will get a compiler error, because the compiler notices that your hashcode() method is not overriding any method from a superclass - so it helps you to catch your error.
 
Wolfgang Tintemann
Ranch Hand
Posts: 65
Eclipse IDE MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you for your clear and exhaustive answers.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!