Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove @Override annotation error in Java8

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.

like image 237
tourist Avatar asked Dec 07 '22 18:12

tourist


1 Answers

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.

like image 50
developer Avatar answered Dec 10 '22 08:12

developer