This question already has answers here:
'Must Override a Superclass Method' Errors after importing a project into Eclipse
(13 answers)
Closed 9 years ago.
I don't know why each time I implements a class. when I use #Override Eclipse always notice error:
the method A of type B must override a superclass method
(With method A is method that I override and B is the current class I work with)
And Eclipse recommend delete #Override
But with the same code, I work on Idea, no error found.
Who can tell me why,please.
thanks :)
#Edit: Oh, I don't post exactly code because It happend for all when I implement sth:
for example:
public class helloworld implements Runnable {
#Override //this line no-error with Idea and error with eclipse:the method run of type helloworld must be override a super class
public void run(){
}
Java 5 only properly supports this annotation on methods you override when subclassing. Starting with Java 6 you can also use this annotation on methods that implement methods from an interface.
Check your compliance setting. It should be JDK 6+
It's a java version issue. In one IDE you are using JDK 6/7 which allow this. In another you are using Java 5, which did not.
Related
Here is my simple code from a Java Tutorial.
public interface AnimalIntf {
default public String identifyMyself(){
return "I am an animal.";
}
}
I get an error: illegal start of type interface methods cannot have body.
The method is default and the default keyword is used at the beginning of the method signature.
Could you please explain to me what is wrong?
Default interface methods were introduced in Java 8, so you need a JDK that supports Java 8 or later.
You must use Java 8 or above to have default implementations in interfaces. Instead you could use an abstract class. But even then, you wouldn't use the default keyword.
Strange things are happening...
After the JDK update to version 8 both on computer and in IDE settings, the code compiles OK, but the IDE still marks the line
default public String identifyMyself(){
as error. And there were still errors while trying to use the interface, asking to override the default method in the class that implements the interface.
public class Dragon implements Animal{
}
2 hours later I got tired of trying to fix my NetBeans v.6.9.1 and downloaded NetBeans v8.0.1.
Now I am fine :)
I know it's supposed to work, but it doesn't. I have a class inheritance defined as follows:
public class VisitManagementForm extends FormWithTitle implements Property.ValueChangeListener {
Now it only allows to override the methods from extends, it doesn't make and indeed doesn't even allow me to implement/override methods from Property.ValueChangeListener. Why is that?
I switched places of implements with extends, then only the implements worked and the extends didn't seem visible.
I recently turned the project to Maven, is it possible Maven is causing this madness? Maybe I need to specify something to allow this with it? Or am I just missing something in here?
Property.ValueChangeListener is supposed to force implement this method like this:
#Override
public void valueChange(ValueChangeEvent event) {
// TODO Auto-generated method stub
}
However now I get the error for it that it needs to override or implement a supertype method...
Edit: I'm using Java 1.6 for the project.
Edit: also if I try to implement something else (while leaving the extends there), it suggests me to implement the methods, but if I implement ValueChangeListener it doesn't suggest me to implement anything, but if you then remove the extends and leave just implements ValueChangeListener, it then suggest to implement it's methods again. But as long as I keep the extends, I seem to get the error mentioned above.
Note that you cannot add the #Override annotation to a method implementing an interface in Java 5 - it generates an error. It is allowed in Java 6
Source
What you are doing is illegal multiple inheritance. Check this example from of Java Language specification
If this is code that worked before you switched to maven, the reason could be that the maven compiler plugin by default sets the source version to 1.5, which means, as already mentioned in another answer, you cannot use #Override for interface methods.
In a repository that was created in Ubuntu Linux and compiled under OpenJDK 6, when implementing LocationListener and SensorListener, adding the #Override tag above the inherited methods compiles fine.
But when the repo is cloned onto a Windows system with Oracle Java, some of the #Override generates errors. Example below:
The method onAccuracyChanged(Sensor, int) of type ExpeditionActivity must override a superclass method
It seems that this error occurs only when you add #Override above a method that is inherited from implementing an interface. Adding #Override above a method inherited from a base class does not generate the error. This actually makes sense, because you are actually overriding the logic of the original method.
SO, is it syactically correct to write #Override above inherited interface methods?
#Override is limited to base class methods in JDK 5.
JDK 6 introduced the ability to apply #Override to interface methods.
Eclipse must be set to use JDK6/JDK6 language level to use #Override on interface methods.
To answer your question, "yes, if you're compiling under JDK 6+".
I'm getting it sometimes.
Not quite sure what the reasons for it are.
A few ways I found of fixing it. Maybe some of them would work for you
Sometimes it helps simply deleting the method decleration and rewriting it.
Make sure the parameters and return value are correct. For example, in Andrdid, when you extend AsyncTask you give the extended class 3 parameters, based on these paratmeters you need to override the methods with the correct parameters and return value.
If the method are required for the interface/base class, try deleting the method. An error would appear under the interface/base class. Put your mouse cursor on it and select Add Unimplemented methods this would make eclipse add these methods and you would have to re-fill the body, but the error would disapper.
I do believe this is an Eclipse error, because removing the #Override annotation doesn't affect the code, the code compiles and runs correctly.
Good luck
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
When do you use Java's #Override annotation and why?
Java, What does #Override mean?
I was checking out Drools Planner example source code and I came across code like this:
#Override
protected Solver createSolver() {
XmlSolverConfigurer configurer = new XmlSolverConfigurer();
configurer.configure(SOLVER_CONFIG);
return configurer.buildSolver();
}
protected Solver createSolverByApi() {
// Not recommended! It is highly recommended to use XmlSolverConfigurer with an XML configuration instead.
SolverConfig solverConfig = new SolverConfig();
solverConfig.setSolutionClass(NQueens.class);
.....TRUNCATED....
solverPhaseConfigList.add(localSearchSolverPhaseConfig);
solverConfig.setSolverPhaseConfigList(solverPhaseConfigList);
return solverConfig.buildSolver();
}
As far as I understand createSolver() and createSolverByApi() are supposed to return Solver objects when you explicitly call them.
What does the #Override mean here? What is the general meaning of the # term?
EDIT: My very bad; I inadvertently duplicated What does #Override mean?
The # is Java Annotations.
The #Override means that the method is overriding the parent class (in this case createSolver).
The Javadoc states for #Override:
Indicates that a method declaration is intended to override a method
declaration in a superclass.
This annotation is useful for compile-time checking to verify that the method you're overriding is valid (overridden correctly).
See the Java tutorial about annotations, and there the 'Annotations used by the compiler' section. A quick copy-paste from the relevant part
#Override—the #Override annotation informs the compiler that the element is meant to override an element declared in a superclass (overriding methods will be discussed in the the lesson titled "Interfaces and Inheritance").
This is called an Annotation. It is actually not compiled into special code, but it helps avoiding errors: essentially, it indicates that the method overrides a method of a superclass. Not having this annotation can cause warnings, having this annotation but no superclass that has a method with the same annotation is even an error.
This avoids refactoring errors: if the method in the superclass is renamed, and the override not, then it will become an error.
This is java annotation , in your case you would use #Override above a method to be sure that you are overriding a super class method , if you use it and the method is not in the super class because you type it name wrong for example an error will occur at compile time .
I want to add this :
declared in a superClass or method declared in an interface (since Java 6)
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
When do you use Java's #Override annotation and why?
My question is very basic why we people use #Override annotation and what is the importance of this annotation?
In Old JDK why it not show as warning, but in latest JDK it required then Why..?
Suppose you have:
public class Foo
{
public void bar(String x, String y) {}
}
public class Foo2 extends Foo
{
public void bar(String x, Object y) {}
}
You really meant Foo2.bar to override Foo.bar, but due to a mistake in the signature, it doesn't. If you use #Override you can get the compiler to detect the fault. It also indicates to any reading the code that this is overriding an existing method or implementing an interface - advising them about current behaviour and the possible impact of renaming the method.
Additionally, your compiler may give you a warning if a method overrides a method without specifying #Override, which means you can detect if someone has added a method with the same signature to a superclass without you being aware of it - you may not want to be overriding the new method, as your existing method may have different semantics. While #Override doesn't provide a way of "un-overriding" the method, it at least highlights the potential problem.
Just to make sure at compile time that we are really overriding the method, also adds good amount to the readability of code
#Override is there since JDK 1.5, so you might get warning in prior
Using this annotation you may be sure that you are really overriding base method, not creating new one (e.g., by accidentally using wrong arguments). If you use #Override annotation, you will get compile time error if something is not right.
To verify during compile time that you are actually overriding a method. IDE's can tell you immediately when you provide that annotation.