Missing @Override in Java libraries

Considered good practice for

use the @Override annotation for methods that override a subclass.

But why the same does not apply to classes that come with the Java library. E.g. String class . It overrides the methods of the Object class, but does not use the @Override annotation for these methods.

This means maintaining backward compatibility with previous versions of Java, such as 1.4, etc.

thanks

+5
source share
3 answers

This is nothing more than a cosmetic annotation; it is useful in creating documentation, gives hints through your Java IDE, and clearly indicates when the method is overridden.

From the point of view of the runtime implementer / standard library, you should not try to modify all existing classes to add something cosmetic.

Also, with regard to backward compatibility of annotations in general, given that annotations are an optional and extended attribute present in the .class file (when their retention policy is either CLASS or RUNTIME and is available to CLASS and Method as Runtime(In)VisibleAnnotations and for Parameter like Runtime(In)VisibleParameterAnnotations ) previous releases of the JVM would simply ignore this attribute when parsing the .class file for the first time a class is required.

But in fact, the 1.4 parser JVM class will not even reach the point where the Annotation .class attribute is inside the structure, because the parsing will end abruptly when the JVM notices that the .class version is larger than supported.

+4
source

Inside the API, it provides little to the user (from this API). However, when you implement a method and you "intend" to override the class of the superclass, it is easy to skip the method signature, which must match.

In this case, @Override comes to the rescue, as during compilation, it will fail or give a warning if there is no override. Many IDEs also recognize @Override and give you enough support to flag and fix these situations before compiling them.

As such, @Override essentially declares its intention that this method redefines something. The user of the API will be less concerned about his intention while he is working.

In fact, probably the true reason is this: Retention @Override annotation is set to SOURCE . This means that the @Override flag @Override discarded when compiled into a class file.

 @Target(value=METHOD) @Retention(value=SOURCE) public @interface Override 
+4
source

the @override annotation is used to provide additional information, mainly when creating documentation, and to inform the developer that the code intends to override a method from a superclass.

This is mentioned in oracle documentation. A.

@Override @Override annotations tells the compiler that the element is intended to override the element declared in the superclass. Overriding methods will be discussed in interfaces and inheritance.

// mark the method as a superclass method // that was overridden @Override int overriddenMethod () {}

Although it is not required to use this annotation when overriding a method, it helps prevent errors. If the method marked with @Override cannot correctly redefine the method in one of its superclasses, the compiler generates an error.

Refer to this discussion in SO itself.

+3
source

All Articles