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.