Do we really need @Override and so on when code Java?
JavaOverridingJava Problem Overview
> 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).
Java Solutions
Solution 1 - Java
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.
Solution 2 - Java
> 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 ...
Solution 3 - Java
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.
Solution 4 - Java
The only difference would be that if you annotate a method with @Override and you are not overriding anything, the compiler will complaint.
Solution 5 - Java
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!
Solution 6 - Java
It's highly recommended to use it since it increase undersatadability of ur code and help others to maintain ur code too .
Solution 7 - Java
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.
Solution 8 - Java
NO!
If you find it useful, you need a real IDE, or learn how to use it.