Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error on @Override annotation with interface implementation

I am using JRE 1.6 version and aware of JRE 1.5 trouble while using @Override with interface.

I imported a new project (Spring + Maven ) and Eclipse giving error on every @Override annotation whenever any interface method is overridden.

Things I tried till now

  1. Checked project and workspace specific JRE and compliance level, It is set to 1.6 version.
    enter image description here
  2. Checked JRE library on build path, it is also same.
  3. Changed Java version in project facet to 1.6 (Dont know if it will help)
    enter image description here
  4. Did Maven clean and install (hundred times till now)
  5. Disabled error / warning for Annotations still no luck enter image description here
  6. Eclipse restart (Stupid thing but helps me lots of time)
  7. Last option will be deleting all .setting and .project files if I dont get anything else to try.

Edit 1:
I am getting following error

The method XXX of type XXX must override a superclass method.        

Edit 2:
Code Sample
Interface declaration

public interface HelperService {   
    public RequisitionTypeDTO getRequisitionTypeDTO(int id) throws Exception;  
}    

Implementation:

   @Service
   public class HelperServiceImpl implements HelperService{   
   @Override  // Getting error for this line
   public RequisitionTypeDTO getRequisitionTypeDTO(int id) throws Exception{
                         // Bla Bla Bla 
      }  
   }     

EDIT 3:
I am able to build and run my application successfully irrespective of this errors. Just not happy with red error flags all over the source code.

like image 210
Ajinkya Avatar asked Jun 08 '12 11:06

Ajinkya


People also ask

Should I use @override when implementing interface?

Such an error is easy to make, and difficult to catch, which is a dangerous combination. Using the @Override annotation prevents you from making such errors. You should be in the habit of using @Override whenever you override a superclass method, or implement an interface method.

What does @override annotation mean?

@Override @Override annotation informs the compiler that the element is meant to override an element declared in a superclass. Overriding methods will be discussed in Interfaces and Inheritance.

What happens if @override is not used Java?

If you don't use the annotation, the sub-class method will be treated as a new method in the subclass (rather than the overriding method). 2) It improves the code's readability.

Can we override method in interface?

You can make the methods default in the interface itself, Default methods are introduced in interfaces since Java8 and if you have default methods in an interface it is not mandatory to override them in the implementing class.


2 Answers

Check if the RequisitionTypeDTO in interface is the same type as RequisitionTypeDTO in implementation (different imports).

If ok then try adding maven-compiler-plugin

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>2.5.1</version>
            <configuration>
                <source>1.6</source>
                <target>1.6</target>
            </configuration>
        </plugin>
    </plugins>
</build>

and Maven->Update project configuration... from context menu of your project - because that is the way you should set compilation jre.

And of course try mvn clean, in Eclipse Project->Clean...

If everything fails create new simple project with minimal code and check if there is the same error.

like image 115
Xeon Avatar answered Oct 15 '22 20:10

Xeon


I got this too, and I did have a "Java Builder" set. Further investigation showed that the problem was that my "Compiler Compliance Level" was set to 1.5 rather than 1.6.

like image 23
Raedwald Avatar answered Oct 15 '22 20:10

Raedwald