How to Fix Must Override a Superclass Method Error Eclipse IDE Java | @Override annotation with interface methods (original) (raw)

One of the annoying error while overriding Java method in Eclipse IDE ismust override a superclass method error, This error is quite frequent when importing Java projects and after so long, still I haven't seen any permanent solution to it. Must override a superclass method the error comes in Eclipse if you have Java Class that implements interface and overrides a method from interface and uses @Override annotation. Unfortunately, Eclipse defaults to Java 1.5, which only recognize @Override annotation for the overriding method from the superclass and not from the interface.

This creates lots of confusion between Java programmers as not everyone is aware of this the subtle difference of using @Override annotation with class and interface methods and they start looking at classpath and path suspiciously. @Override annotation for interface methods are only available from Java 1.6 and if you use @Override while overriding interface method then you will get must override a superclass method error.

In this Java and the Eclipse tutorial we will see how to the fix must override a superclass method error in eclipse but before that we will reproduce this error in a manner that will clear that Eclipse will complain only about methods that are overridden from the interface.

Reproduce must override a superclass method error in Eclipse

In order to reproduce must override a superclass method error Just create an Eclipse project with compiler settings pointing to Java 1.5. Here is an example class for reproducing "must override a superclass method error" :

public classHelloWorld extends Thread{

public static voidmain(String args[]){
System.out.println("Helloworld! version 2");
}

@Override
public void run(){ //no error in this line

}
}

class Test implements Runnable{

@Override
public void run(){ //eclipse will show "must override a superclass method error"
}
}

if you paste this Java program in Eclipse IDE with Java source settings 1.5 you will get an error inside the Test class run() method while HelloWorld class which overrides run() method from java.lang.Thread superclass will not throw any error.

How to fix must override a superclass method error

Fixing must override a superclass method error is not difficult, You just need tochange Java source version to 1.6 because from Java 1.6 @Override annotation can be used along with the interface method. In order to change the source version to 1.6 follow the below steps :

  1. Select Project, Right-click, Properties

  2. Select Java Compiler and check the checkbox "Enable project specific settings"

  3. Now make the Compiler compliance level to 1.6

  4. Apply changes

Here is a screenshot of changing compiler settings to use Java 1.6 to fix must override a superclass method error.

must override a superclass method Eclipse Java solution

That's all on How to fix must override a superclass method error in Eclipse IDE. As soon as you do this change and if your build is set to automatic than Eclipse stops showing "must override a superclass method error" for any overridden method from the interface. I know it's pretty annoying and this subtle detail about @Override annotation is not obvious. let me know if you face ""must override a superclass method error" because of any other reason in Eclipse.

Other Java and Eclipse tutorials from Javarevisited Blog