7

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

Umberto Raimondi
  • 16,269
  • 7
  • 44
  • 54
Ankit
  • 231
  • 2
  • 9
  • Sir, this question of mine is not duplicate of the question When do you use Java's Override annotation and why? Because I needed to ask why Override isn't used in Java Libraries – Ankit May 05 '15 at 09:23
  • 1
    @Ankit The libraries came first, the annotations came later. They probably didn't have a graduate or intern around to go through the entire library code just to add override annotations :P – MadProgrammer May 05 '15 at 09:26
  • @Ankit you might be right - seems to be sufficiently different - dropping the claim. – YoYo May 05 '15 at 09:30

3 Answers3

5

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
YoYo
  • 7,796
  • 8
  • 50
  • 67
4

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.

Umberto Raimondi
  • 16,269
  • 7
  • 44
  • 54
2

@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.

Community
  • 1
  • 1
Yadu Krishnan
  • 3,182
  • 1
  • 36
  • 74