62

Possible Duplicate:
When do you use Java's @Override annotation and why?

I wonder what the functionality of adding @Override in front of the code we would like to override is. I have done with and without it, and it seemed that everything was just going well (at least, for me).

Community
  • 1
  • 1
zfm
  • 1,906
  • 2
  • 17
  • 28
  • 10
    You get much nicer compile time checking, for one. Duplicate: http://stackoverflow.com/questions/94361/when-do-you-use-javas-override-annotation-and-why – wkl Jan 27 '11 at 23:00
  • 2
    I was so relived when switched from Delphi to Java I didn't need 'virtual' any longer... @Override is a step back. Nowadays most IDEs have auto-override, so it's rarely written by hand, contributes to the fluffy java style, though :) – bestsss Jan 28 '11 at 01:00
  • @bestsss - nobody forces you to use @Override. But don't you dare strip it out of any code that >>I<< write. – Stephen C Jan 28 '11 at 01:26
  • @Stephen C: ugg, i know i am getting old and my memory is fading but I dont recall doing so. – bestsss Jan 28 '11 at 01:32
  • I don't think this duplicates other claimed pages: I don't see them explicitly speaking to the *necessity*, which is the particular focus of this distinct question. – cellepo Aug 14 '23 at 22:57

8 Answers8

73

It is not necessary, but it is highly recommended. It keeps you from shooting yourself in the foot. It helps prevent the case when you write a function that you think overrides another one but you misspelled something and you get completely unexpected behavior.

James Kingsbery
  • 7,298
  • 2
  • 38
  • 67
44

what the functionality of adding @Override

It lets the compiler double-check for you when you say (by annotating) that a specified method is supposed to override a superclass method (or implement an interface method in Java 6 or later). If the method does not, in fact, override a superclass method (or implement an interface method), the compiler will flag this as an error. This often indicates that you have a typo in the method name or made a mistake in the method signature.

Do we really need @Override

Need it? Absolutely not, but its such a cheap way to

  • conveys explicitly to human reader that this is an overriding method, and
  • catches a bug at compile time that could take at least a few brain cycles to catch at run-time once you even know to look for it

... and even cheaper when your IDE is helping you include it ...

Bert F
  • 85,407
  • 12
  • 106
  • 123
7

It's recommended since it helps to manage consistence. Imagine someone will change the name of superclass method (and only there, without performing name-changes in classes depending on it which is quite hypothetical :) ), then you will be the first one to know due to compilation errors.

Art Licis
  • 3,619
  • 1
  • 29
  • 49
6

The only difference would be that if you annotate a method with @Override and you are not overriding anything, the compiler will complaint.

Check When do you use javas override annotation and why?

Community
  • 1
  • 1
jhurtado
  • 8,587
  • 1
  • 23
  • 34
2

It's helped me a good couple of times to avoid spelling errors / case differences in method names. Without it it's one of these annoying bugs that can take ages to track down.

It doesn't add any functional difference, but telling the compiler you think you're overriding a method so it can complain if you're not is very useful when you've screwed up somewhere!

Michael Berry
  • 70,193
  • 21
  • 157
  • 216
1

It's highly recommended to use it since it increase undersatadability of ur code and help others to maintain ur code too .

palAlaa
  • 9,500
  • 33
  • 107
  • 166
-4

Personally I dont think it is useful except for IDE's that do the compile-time error checking, but that is my opinion. Reason: suppose you have

Class A { 
    methodName() { System.out.println ("A"); }
}

class B extends A { 
    methodName() { System.out.println ("B"); }
}

At runtime, you will probably call B.methodName() - it doesnt matter to B whether B.methodName() overrides the same name of class A(). To class B, it shouldn't even matter if the superclass (A) implements methodName(). What I mean is that inheritance is a one-way street - You cannot un-inherit something by using a @override - all the compiler can check is if there is a method with the same signature in the superclass - which will not be used anyways.

For the other answers, if someone edits A.java to delete or rename methodName() or change its signature, you can still call B.methodName() without a problem, but only if you do not use that @override. I think this is also why it is not a part of the Java language.

Manidip Sengupta
  • 3,573
  • 5
  • 25
  • 27
-8

NO!

If you find it useful, you need a real IDE, or learn how to use it.

irreputable
  • 44,725
  • 9
  • 65
  • 93