I am not clear on @override annotation. Let me explain with a scenario.
In the above code, when MyFirstClass extends an abstract class and implements a method getNumber(), using @override is fine.
But in MySecondClass which is implementing MyInterface and implementing the method getNumber(), using @override gives a compilation error.
My confusion is even all the methods in the interface are internally abstract also the interface itself is an abstract to the compiler. Why is @override tag used only when implementing methods of abstract classes but not when implementing interface methods.
I think that this is unnecessarily confusing, actually. @override is useless when you're "overriding" an abstract method. The usefulness of @override is when you're overriding a non- abstract method, but you misspell the method name or change the parameter list, and as a result, you don't really override it. Then you can get a warning or error. Since making the same mistake when overriding an abstract method would result in a compile error anyway, it's really not useful, and they could have (in theory, at least) defined this annotation to only be applicable to overrides of non-abstract methods.
Originally posted by Ernest Friedman-Hill: @override is useless when you're "overriding" an abstract method.
No, it's not.
Imagine you introduce a new abstract method, and accidentally a subclass already has a method with the same signature. Would be good to notice, wouldn't it?
The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
author and iconoclast
IntelliJ has the option as well, under Errors -> IDE Profiles -> Inspections -> Class Structure -> Missing @Override annotation.
If one wanted to enforce this as part of build, I imagine there's a way to do it with apt or a CheckStyle extension. Seems like there probably already is such a tool somewhere, but my brief round of googling didn't turn it up.