Saturday, February 2, 2013

How to Fix Must Override a Superclass Method Error Eclipse IDE Java | @Override annotation With interface methods

One of annoying error while overriding Java method in Eclipse IDE is must override a superclass method error , This error is quite frequent when importing Java  projects and still I haven't seen any permanent solution of it. Must override a super class method error comes in Eclipse, if you have Java Class that implements interface and overrides method from interface and uses @Override annotation . Unfortunately Eclipse defaults to Java 1.5, which only recognize @Override annotation for overriding method from super class and not from interface. This creates lots of confusion between Java programmers as not every one is aware of this subtle difference of using @Override annotation with class and interface methods and they start looking 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 than you will get must override a superclass method error. In this Java and Eclipse tutorial we will see how to fix must override a superclass method error in eclipse but before that we will reproduce this error in a manner which will clear that Eclipse will complain only for methods which is overridden from 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 class HelloWorld extends Thread{

        public static void main(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 error inside 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 to change Java source version to 1.6 because from Java 1.6 @Override annotation can be used along with interface method. In order to change source version to 1.6 follow below steps :

1) Select Project , Right click , Properties
2) Select Java Compiler and check the check box "Enable project specific settings"
3) Now make Compiler compliance level to 1.6
4) Apply changes

Here is screen short 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 interface. I know its 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

4 comments :

Jiri Pinkas said...

Eclipse defaults to Java 1.5? When? I did not encounter this error for a long time now.

One minor tip: if you change Java compiler version and your project has facets, you may also want to change Java version in "Project Facets", otherwise you will see this error:

Java compiler level does not match the version of the installed Java project facet.

Javin @ ClassLoader in Java said...

Hi @Jiri, Thanks for comment. Well it depends upon default compiler compliance level of your workspace, should have mentioned that specifically. Because of Java 1.5 as default compiler compliance level, any imported or new Java project was defaulting to Java 1.5, even though it has JRE 1.6 as installed JRE.

Anonymous said...

Hi Javin,
Thanks for nice posts. Few days back i faced the same kind of situation, tried to solve but didn't get solution. So simple removed '@Override' :) :) continued work.

Anonymous said...

That's easy just remove @ override annotation . Why bother too much but imagine if you have to do that on numerous classes , in tht case I thing it still better to move to java 6 rates than sticking with java 5 agree?

Post a Comment