13

I'm writing a Java class that's implementing an interface called Command, which contains the methods isValid() and run(), as follows:

public class DailyEnergy implements Command {

  @Override
  public boolean isValid(String command) {
    return false;
  }

  @Override
  public void run(String command) throws Exception {
  }
}

and here's the Command.java file:

public interface Command {

  public boolean isValid(String command);
  public void run(String command) throws Exception;
}

Within this class, I'm implementing the superclass methods isValid() and run(), and I want to add the @Override annotation, but Eclipse gives an error saying that "the methods must override superclass methods".

Even when I take out the methods and import them automatically with Eclipse, if I add the annotation, I get the error. If anyone can shed some light as to why I can't use the @Override annotation, that would be greatly appreciated.

TNC
  • 337
  • 2
  • 5
  • 17

2 Answers2

46

The @Override annotation on interface implementations is supported since Java-6. Are you possibly on Java-5? Oracle has acknowledged a mess-up in the Java 6 docs. It has been corrected in Java-7. See example below:

Check compiler setting

Sri Sankaran
  • 8,120
  • 4
  • 38
  • 47
  • 5
    I'd check once again that the Project's and/or default Eclipse JDK is not set to JDK 5. – Piotr Nowicki Nov 22 '11 at 12:57
  • My goodness, you're right, that was the problem. I guess Eclipse defaults to JDK 5, since I never had Java 5 installed at any point.. Strange, because I swore I never had this problem before. But anyways, thank you so much! – TNC Nov 22 '11 at 17:35
2

Do you use JDK5? As I have in mind is that , it's a bug in JDK5. @override is not allowed in implemention of interface in JDK5

biaobiaoqi
  • 1,098
  • 12
  • 18