Remove @Override annotation error in Java8

sbk picture sbk · Mar 26, 2017 · Viewed 12.3k times · Source

I created the following interface with a single method:

public interface Greeting {

   public void perform();
}

I was trying to pass an instance of the interface to the greet() method.

public class Greeter {

  public void greet(Greeting greeting){
    greeting.perform();
 }

 public static void main(String[] args) {       
    Greeter greeter=new Greeter();
 }
}

I created an implementation of Greeting Interface:

public class HelloWorldGreeting implements Greeting {

 @Override
 public void perform() {
    System.out.println("Hello World");

 }
}

I was getting the error(The method perform() of type HelloWorldGreeting must override or implement a supertype method) with a quick fix as "Remove @Override annotation"

I also checked compiler configuration. It's 1.8 only.

enter image description here

Please let me know why is the error coming or help me fix it. Thanks in advance.

Answer

developer picture developer · Mar 26, 2017

Please let me know why is the error coming or help me fix it?

This can happen when the latest class files are not being generated i.e., the override method signature is not matching because the class files are still referring to old. So, ensure that the project is built properly and the latest class files are being generated.

In eclipse, Project (at the top bar) -> clean and check whether the latest class files are being generated (in the project/target/classes folder or any other which you have configured). Also, ensure that there are no other errors in the project which might prevent the class files generation. You can also look here on why project can't be built.