Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java: Is the Override annotation useless with modern IDEs?

Is it just me, or is Java's Override annotation actually useless when programming in a decent IDE?

Yes, I understand that the Override annotation prevents me from accidentally not overriding a super method by misspelling the method name or messing up the method parameters. However, common sense tells me this is very unlikely to happen in modern IDEs, for the following reasons:

  • The IDE shows a marker (i.e. a small icon) on the left side of the editor that indicates whether the method is overridden/overriding. Therefore, no explicit Override annotation is needed to show the Override intention.
  • If I'm about to rename a method and I see such an override marker, I never, ever edit the method name directly - I instinctively reach out for the keyboard shortcut that opens the IDE's renaming assistant. Likewise, if I intend to change the method signature and I see the override marker, I instinctively use the IDE's method signature assistant instead of doing it by hand (except for very simple cases). Method names and signatures are thus almost always changed correctly, so adding Override annotations becomes (in my opinion) little more than a paranoid and useless safety check - same as spreading 'final' all over the place.

Sure, if you have to write your Java code in Notepad or whatever, the Override annotation obviously helps. But my point is, in a modern IDE, the increase in safety provided by this annotation is not worth the decrease in readability. Any opinions?

like image 415
python dude Avatar asked Jun 20 '11 07:06

python dude


People also ask

Is override annotation necessary in Java?

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

What happens if you don't use @override Java?

It will throw a compilation error if you have the annotation on a method you're not actually overriding the superclass method. The most common case where this is useful is when you are changing a method in the base class to have a different parameter list.

Is override annotation optional in Java?

So really, it's not optional, it's not there at all from the language perspective. Some people do want the feature, even if the language doesn't have it, and they added the @override annotation and analyzer support.

Is it mandatory to use @override?

@Override annotation is used when we override a method in sub class. Generally novice developers overlook this feature as it is not mandatory to use this annotation while overriding the method.


1 Answers

Well you have to trust yourself to see those markers. There is a human element to it and humans are fallible. From personal experience I often tend to overlook those warnings and 'markers'. On the other hand, the compiler is far more exacting and if you annotate a method with Override it will make sure that you are really overriding a superclass method.

like image 179
aldrin Avatar answered Nov 14 '22 22:11

aldrin