One of the 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 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.
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 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"
}
}
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 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 to
change 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.
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
6 comments :
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.
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.
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.
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?
you are amazinggggggg! this worked so well
Hi Javin
Thanks a lot. It worked smoothly and removed the error for which I had been searching a solution for quite some time.
Post a Comment