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.
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
Tidak ada komentar:
Posting Komentar