Reputation: 13915
I have
ISomeInterface
public interface ISomeInterface{
public void myMethod();
}
AbstractClass
public abstract class AbstractClass implements ISomeInterface{
public void myMethod(){
//...here goes implemetations
}
}
ConcreteClass
public class ConcreteClass extends AbstractClass {
//...
}
Compiler prints than ConcreteClass
is not abstract and does not override abstract method myMethod
in ISomeInterface
.
The idea is to give implementation to one abstract class and then inherit it in classes that extend it.
I think that ConcreteClass
should gets implementation form AbstractClass
since it's extending it. Right? What's the matter?
UPDATE
I haven't noticed until now that method
is wrong and it has to be myMethod
. Anyways, same error.
UPDATE2
The problem was that in AbstractClass them method had correct name but incorrect signature. After changing it according with interface the problem was solved :)
Upvotes: 1
Views: 237
Reputation: 19231
You are probably overriding the wrong method. What happens is that you are probably attempting to override a method in your abstract class but what you are actually doing is to just define a new method with a new name. In the interface, the method is named method
but in your abstract class your method is named myMethod
.
So, check this out:
public abstract class AbstractClass implements ISomeInterface{
// Not the same name as in the interface
public void myMethod(){
//...here goes implemetations
}
}
In order to solve it, simply change the method name in the subclass to the correct name.
public abstract class AbstractClass implements ISomeInterface{
// Now you have the correct name and inheritance will
// work as expected
@Override
public void method(){
//...here goes implemetations
}
}
This is the perfect case for explaining the @Override
annotation as well ;)
When overriding a method, you might want to use the @Override annotation that instructs the compiler that you intend to override a method in the superclass. If, for some reason, the compiler detects that the method does not exist in one of the superclasses, then it will generate an error.
When you declare a method with the annotation @Override
the overridden method must match the signature of the interface-method (or superclass method). Read more about @Override
in the Oracle Docs.
And, if you are not trying to override the method named method
in your abstract class you simply need to add that method to your concrete class like this:
public class ConcreteClass extends AbstractClass {
// Now we are implementing the correct method from the interface
// If not, there will be a compiler error.
@Override
public void method() {
}
//...
}
On a side note which may be relevant: methods can have the same name but with different argument lists. This is known as overloading (or overloaded methods) which you can read more about in this article.
Edit: Since the OP is using Java 5 this question may be interesting. The @Override
annotation changed between Java 5 and Java 6. In Java 5 it was not allowed to use the @Override
annotation when implementing a method from an interface, it was just allowed when overriding a method from a superclass.
Upvotes: 0
Reputation: 19171
Assuming that your sample code is complete, you need to implement method
from your interface. In AbstractClass
you have called it myMethod
instead.
You can use the @Override
annotation on any override methods. This will tell you if you have used the wrong parameter types, method name, or an incompatible return type (parameters and return types don't have to be the same; they can, for example, be covariant).
Additionally, in Java, we don't tend to prefix interface names with I
(unlike the convention in C#). Your interface would usually be called SomeInterface
and not ISomeInterface
.
Also, the public
on methods on interfaces is implicit, so you can leave it out. If you do include it you should probably include the abstract
as well to include all of the implicit modifiers.
An updated code sample would be:
public interface SomeInterface{
void method();
}
public abstract class AbstractClass implements SomeInterface {
@Override
public void method(){
//...here goes implemetations
}
}
Upvotes: 2
Reputation: 356
Well the first thing that I notice is that your method is not named the same thing in the interface and the abstract class.
Upvotes: 0
Reputation: 201527
Your interface defines
public void method();
but your abstract class has
public void myMethod(){
//...here goes implemetations
}
Which isn't the same! Add the Override annotation to catch that kind of issue at compile time.
@Override
public void myMethod(){ // <-- compile error.
//...here goes implemetations
}
From the linked Javadoc,
Indicates that a method declaration is intended to override a method declaration in a supertype.
Upvotes: 0
Reputation: 10810
In AbstractClass
you are creating a method myMethod()
, but your interface method method()
is not being implemented in ConcreteClass
. The names are different.
Upvotes: 2