JDK5 Annotations; I love them, no, I hate them! (Actually I hate Eclipse) :)
So there is a fantastically useful annotation: @Override which when indicates that a particular method is overriding a method in the super class.
Why is this useful?
Assume today we are overriding the method called "methodA", tomorrow we get a new release of the library and the method in the parent class is no longer called "methodA", but renamed to "methodWithAMuchBetterName".
Your code is syntactically correct; it will compile, but it is not semantically correct. The no longer overridden method fails. No compiler errors to help you out; nothing.
You may argue that if you are overridding a method you almost certainly want to be calling super.sameMethod(); but that is just wrong. You do not have to , nothing in the million + 1 best practices suggest it, it is one of the more annoying anti-patterns. In particular; a lot of framework provide template method which do absolutely nothing for the sole purpose of you extending them; or they provide abstract methods for the same purpose.
Anyways; so JDK5 introduced an Annotation which told the compiler that the method is overridding a parent method. If the method in the super class is removed; your code won't compile.
Excellent. Lovely. As well as adding extra documentation to the source code; it has empowering side effects.
So sure; I love JDK annotations.
Now I implement an interface; create a method which implements the interface, stick an "@Override" on the method, and what happens? Eclipse moans that the method must override a superclass method.
ARRRGGHHH!!!!! It still compiles, but the source code looks like it is incorrect code.
Poo.
Matthew Jones
I guess in my (very) limited knowledge of Java, Eclipse is being clever because technically you're not overriding a method, you're implementing it. If there was an annotation for that as well, that would be cool.
Then again I could be talking out of my proverbial backside. It's been a while since I used Java5.
03 Feb 2006, 14:23
Mathew Mannion
Java5 is lovely, I'm going to make all of Blogbuilder Java5 fantastic soon! Then I'll cry.
03 Feb 2006, 14:25
Irfan Cehajic
I just had the same problem with the @Override, and this is what I figured out:
@Override should be used when you’re overriding methods from extended classes. Don’t use @Override if you’re implementing interface methods.
15 Jun 2007, 16:01
David Lim
That’s rather obvious isn’t it? You override a superclass method. You don’t override an interface method. Eclipse will give you a warning if you forget the @Override tag for overridden methods so you don’t really need to bother to manually add them when you can use the quick fix on the warning.
15 Aug 2007, 08:33
Add a comment
You are not allowed to comment on this entry as it has restricted commenting permissions.