17

I don't know why each time I implements a class. when I use @Override Eclipse always notice error:

the method A of type B must override a superclass method

(With method A is method that I override and B is the current class I work with)

And Eclipse recommend delete @Override

But with the same code, I work on Idea, no error found.

Who can tell me why,please.

thanks :)

@Edit: Oh, I don't post exactly code because It happend for all when I implement sth: for example:

public class helloworld implements Runnable {
    @Override //this line no-error with Idea and error with eclipse:the method run of type helloworld must be override a super class
    public void run(){
    }
hqt
  • 27,058
  • 46
  • 161
  • 235
  • 2
    How about pasting here the code that generates the error? – Óscar López Feb 09 '12 at 03:38
  • I second Oscar's advice -- which should be obvious, no? It's like you're asking us to read your mind and guess the code that's causing the error. – Hovercraft Full Of Eels Feb 09 '12 at 03:39
  • class A inherits from class B and override a method of class B? publishes the code – Jhonathan Feb 09 '12 at 03:42
  • 2
    What JDK compliance is your Eclipse Java compiler set to? I think that override annotations were not allowed for interface methods in some earlier versions of Java. – Hovercraft Full Of Eels Feb 09 '12 at 03:51
  • @Hovercraft Full Of eels I have updated my post – hqt Feb 09 '12 at 03:52
  • I'm using JDK 7. And at least, JDK version same with Eclipse and Idea – hqt Feb 09 '12 at 03:54
  • 3
    @hqt: and I have asked another question. Again, what jdk compliance is your Eclipse Java compiler set to? You check by going to the Windows -- Preferences -- Java -- Compiler menu. And no, I'm not asking your Java *version* but your Eclipse setting on JDK *compliance*! There's a big difference! – Hovercraft Full Of Eels Feb 09 '12 at 03:54
  • Would JDK compliance level really affect something as fundamental as an @override annotation? I bet the OP's eclipse is simply in a bad state and needs to be restarted. I've seen this happen before. A "refresh" on the source tree or a restart fixes all these bogus errors that show up. – eternaln00b Feb 09 '12 at 04:00
  • 1
    @SiddharthaShankar Actually yes, I had exactly this issue come up a couple of times very recently, and that was the fix. – Jeff Feb 09 '12 at 04:04
  • @Jeff, changing the compliance level was the fix, or was it restarting Eclipse? – eternaln00b Feb 09 '12 at 04:05
  • 1
    @SiddharthaShankar changing the compliance level. – Jeff Feb 09 '12 at 04:05
  • ah, I see. With some searching, I find that the @override annotation was a relatively recent introduction. Hmmm... It would be nice if people posted the rationale for their suggestions for some of us less enlightened folks. :) – eternaln00b Feb 09 '12 at 04:08
  • 1
    @SiddharthaShankar Actually you might just need to read a little closer: From Hovercraft Full Of Eels comment: `I think that override annotations were not allowed for interface methods in some earlier versions of Java.` – Jeff Feb 09 '12 at 04:12
  • @Jeff, my mistake again! I read his later comment, but not the earlier one. Apologies all round. – eternaln00b Feb 09 '12 at 04:14
  • @SiddharthaShankar: accepted. – Hovercraft Full Of Eels Feb 09 '12 at 04:16
  • Oh, thanks. I have fixed my problem (thanks to Hovercraft Full Of eels). Here is his post: (I post again for overall answer) Again, what jdk compliance is your Eclipse Java compiler set to? You check by going to the Windows -- Preferences -- Java -- Compiler menu. And no, I'm not asking your Java version but your Eclipse setting on JDK compliance! – hqt Feb 09 '12 at 05:16
  • Go to windows-> preferences -> Java -> compiler -> Select compiler compliance level to 1.6(which version you want)-> Apply-> ok. now its work. – madhu Dec 30 '14 at 14:17

3 Answers3

8

Java 5 only properly supports this annotation on methods you override when subclassing. Starting with Java 6 you can also use this annotation on methods that implement methods from an interface.

Vadim
  • 4,663
  • 1
  • 23
  • 29
5

Check your compliance setting. It should be JDK 6+

manocha_ak
  • 874
  • 7
  • 19
2

It's a java version issue. In one IDE you are using JDK 6/7 which allow this. In another you are using Java 5, which did not.

MJB
  • 9,088
  • 6
  • 30
  • 48