It is considered to be a good practice to
use @Override annotation on methods which are being overriden in subclass.
But why is same not applied to the classes that come with Java Library. For e.g. String Class. It overrides methods of Object class but does not use @Override annotation on these methods.
Is this so to maintain backward compatibility with previous releases of Java such as 1.4 etc.
Thanks
If you don't use the annotation, the sub-class method will be treated as a new method in the subclass (rather than the overriding method).
@Override @Override annotation informs the compiler that the element is meant to override an element declared in a superclass. Overriding methods will be discussed in Interfaces and Inheritance. While it is not required to use this annotation when overriding a method, it helps to prevent errors.
@Override is optional in Java. All it does is validate that a superclass method with the same signature exists. It doesn't work in the other direction. class A implements Foo { ... }
In any case, the use of @override is optional. For example, the annotation is intentionally not used in the Dart platform libraries, since they only depend on themselves.
Within an API, it does not offer much to the user (of that API). However when you implement a method, and you 'intend' do override that of a super class, it is easy to miss out on the method signature, which is supposed to match.
In this case the @Override
comes to the rescue as at compile time, it will fail or give a warning when the override does not happen. Also many IDE's recognize the @Override
and give you enough support to flag and correct those situations before you even compile.
So the @Override
in essence declares your intention that this method overrides something. The user of the API would care less what your intent is, as long as it works.
Actually, probably the true reason is this: The Retention of the @Override
annotation is set to SOURCE
. Which means the @Override
flag is discarded when compiled into a class file.
@Target(value=METHOD)
@Retention(value=SOURCE)
public @interface Override
That is not much more of a cosmetic annotation, it's useful when generating documentation, to give hints through your Java IDE and to explicitly state when a method is overriden.
From the runtime/standard library implementors point of view, it was not worth the effort to modify all existing classes just to add something cosmetic.
Furthermore, regarding backward compatibility of annotations in general, considering that annotations are an optional and extended attribute present in .class
file (when their retention policy is either CLASS
or RUNTIME
and available for Class
and Method
as Runtime(In)VisibleAnnotations
and for Parameter
as Runtime(In)VisibleParameterAnnotations
) previous releases of the JVM would simply ignore that attribute during the .class
file parsing performed the first time that Class is needed.
But actually, that 1.4 JVM class parser will not even reach the point where those Annotation .class
attribute are located inside the structure because the parsing will end abruptly when the JVM will notice that the .class
version is greater than the supported one.
@override annotation is used to provide some extra information, mainly while generating documentations and also for informing the developer that the code intends to override a method from the superclass.
This is mentioned in oracle documentation.
@Override @Override annotation informs the compiler that the element is meant to override an element declared in a superclass. Overriding methods will be discussed in Interfaces and Inheritance.
// mark method as a superclass method // that has been overridden @Override int overriddenMethod() { }
While it is not required to use this annotation when overriding a method, it helps to prevent errors. If a method marked with @Override fails to correctly override a method in one of its superclasses, the compiler generates an error.
Refer to this discussion in SO itself.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With