Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java Eclipse @Override error

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.

like image 939
TNC Avatar asked Nov 22 '11 01:11

TNC


People also ask

What happens if @override is not used Java?

While it is not required to use this annotation when overriding a method, it helps to prevent errors. If a method marked with @Override fails to correctly override a method in one of its superclasses, the compiler generates an error.

What does Java @override mean?

The @Override annotation is one of a default Java annotation and it can be introduced in Java 1.5 Version. The @Override annotation indicates that the child class method is over-writing its base class method.

Is @override mandatory Java?

In Java, annotations are the metadata that we used to provide information to the compiler. Here, the @Override annotation specifies the compiler that the method after this annotation overrides the method of the superclass. It is not mandatory to use @Override .


Video Answer


2 Answers

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

like image 85
Sri Sankaran Avatar answered Sep 28 '22 16:09

Sri Sankaran


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

like image 35
biaobiaoqi Avatar answered Sep 28 '22 15:09

biaobiaoqi