February 03, 2006

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.


- 4 comments by 2 or more people Not publicly viewable

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

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

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

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

February 2006

Mo Tu We Th Fr Sa Su
Jan |  Today  | Mar
      1 2 3 4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28               

Search this blog

Tags

Galleries

Most recent comments

  • Interesting… While I'm not completely convinced in such microbenchmarks, I'm pretty sure that 1ms … by Alexander Snaps on this entry
  • Hello. I bought the book yesterday. I was trying to find the source code for chapter 11 and chapter … by Suleman on this entry
  • http://woosight.net/account/login?username=demo by live mashup demo on this entry
  • Thanks mate ….. This blog was really helpful. by Maaz Hurzuk on this entry
  • Ty. Not directly helpful for my problem, but pointed me in the right direction. You will also get th… by Mike E. on this entry

Blog archive

Loading…
Not signed in
Sign in

Powered by BlogBuilder
© MMXXI