Today I have ported a Java 6 project back to Java 5. This led to compiler failures in Eclipse, but not in Maven which seemed quite strange at first glance. Interestingly, they are caused by the @Override
annotation.
The Java 5 API for @Override
says:
- Indicates that a method declaration is intended to override a method declaration in a superclass. If a method is annotated with this annotation type but does not override a superclass method, compilers are required to generate an error message.
Note that it says "superclass", not "supertype". Hence, it's not allowed to add this annotation to methods that implement methods of an interface. Javac (which is called by Maven) does not report this as an error, but the Eclipse compiler does.
Well, if you take a look at Java 6, the API didn't change at all so I was surprised to see a different behavior: the @Override
annotation is allowed for methods implementing interface methods in Javac, too. In the end, I had to remove those annotations to make the code compile with Java 5 in Eclipse.
After some googling, I found out that this has just been forgotten by Sun developers: the compiler's behavior is changed but the documentation does not reflect that (see here). And indeed, when you look at the API of @Override
in upcoming Java 7 it looks like:
- Indicates that a method declaration is intended to override a method declaration in a supertype. If a method is annotated with this annotation type compilers are required to generate an error message unless at least one of the following conditions hold:
- The method does override or implement a method declared in a supertype.
- The method has a signature that is override-equivalent to that of any public method declared in Object.
Here you got it: @Override
may now bee used for interface methods, too.
Hi, I am seeing this problem in my project. I am compiling with eclipse. So do you have a solution to overcome this error? My computer has java 1.6.0_22
ReplyDeletethanks!
Hi Linda,
ReplyDeleteas I've written, in Java 6 those @Override annotations are allowed for overriding interfaces, too, albeit the documentation does not reflect that. So what exactly are the problems you see with JDK 1.6.0_22 in Eclipse?
Hi, I am designing a program for matrix operations in java using rmi technology.
ReplyDeleteThe implementation part of my interfaces always shows (The method does not override or implement a method from a super type)
What should I do?
Hi, I am designing a program for matrix operations in java using rmi technology.
ReplyDeleteThe implementation part of my interfaces always shows (The method does not override or implement a method from a super type)
What should I do?