What are the benefits of automatically generating @override annotations in the IDE?

I use eclipse when I use a shortcut to create overridden implementations, there is an overriding annotation there, I use JDK 6, everything is fine, but under JDK 5 this annotation will lead to an error, so I want to ask if this annotation is completely useless? Will the compiler do some kind of optimization using this annotation?

+4
source share
5 answers

As others noted, the @Override annotation is really a compiler directive that instructs javac scream if the method annotated with @Override does not actually override the method in its parent class (for example, you are actually overloading because you decided to change the signature method or skip method name).

In JDK 5, the direct implementation of a method from an interface is not considered to override this method and is considered an error if annotated using @Override .

Partly due to user feedback that this was a really confusing behavior, JDK 6 changed this behavior and considers it appropriate to annotate the method that you implement from the interface with @Override .

+3
source

Its purpose is for the compiler to tell you when a method does not actually override a superclass method. For example, suppose you mistype a name, with annotation, the compiler will warn you that the method does not cancel anything, and therefore you will be able to catch your mistake, instead of launching your program and not understand why your method never gets called.

+4
source

Annotations are very valuable in the sense that it will become crystal clear, that the new method will execute or not override the method of the parent class. For example, you might think that you are redefining, but you mistakenly wrote the name of the method (or that the redefined method signature changed at the same time).

+3
source

It is not useless. This helps the reader understand the code and the author in order to avoid mistakes.

However, JDK 5 and JDK 6 behave differently with @Override s, so just delete them if they cause problems. They have absolutely no functional difference.

+3
source

Compile the code with the JDK you are deploying.

The problem is that the annotation is valid against overriding methods from the base class and methods defined in interfaces in version 6, but only against overriding methods in version 5. Thus, @override in methods defined in the interface will cause your error in the JDK 5.

+2
source

Source: https://habr.com/ru/post/1304140/