1

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 :)

J.Olufsen
  • 13,415
  • 44
  • 120
  • 185
  • 1
    Using the `@Override` annotation on `myMethod` in `AbstractClass` would helped identify this error. – rgettman Jan 30 '15 at 22:45
  • That's weird. Which compiler are you using? With the official compiler from Oracle, it compiles without errors – BackSlash Jan 30 '15 at 22:47
  • 2
    You have changed your question now and say that you get the same error if you have the same method name in both the interface and the class. If you have the same method name with the same case-sensitivity you won't get an error about it not being implemented in ConcreteClass. Check your update to your code carefully. – Andy Brown Jan 30 '15 at 22:52

5 Answers5

2

In AbstractClass you are creating a method myMethod(), but your interface method method() is not being implemented in ConcreteClass. The names are different.

Kon
  • 10,702
  • 6
  • 41
  • 58
2

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
  }
}
Andy Brown
  • 18,961
  • 3
  • 52
  • 62
0

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.

Community
  • 1
  • 1
wassgren
  • 18,651
  • 6
  • 63
  • 77
  • So `method()` **have** to be overriden in AbstractClass in order to implement interface? It compiles without `@Override` annotations – J.Olufsen Jan 30 '15 at 22:59
  • 1
    @RCola No. A method that doesn't match a super-type signature will fail to compile if it has that annotation. – Elliott Frisch Jan 30 '15 at 23:00
  • @RCola, Thx Elliot. If you add `@Override` you'll guarantee that the method you are implementing is actually overriding a super class or interface method (i.e. a method with the same signature). – wassgren Jan 30 '15 at 23:01
  • @RCola, furtermore: if you add _any method_ to the abstract class with a different signature it is **not** an override. Signatures are based on method name, parameters, exception list and so on. That is why adding `@Override` is a good idea (if you are overriding). Then you **know** that you are actually overriding the intended method. – wassgren Jan 30 '15 at 23:04
  • But if I use Java 5 I cannot apply `@Override` annotation. – J.Olufsen Jan 30 '15 at 23:11
  • My IDE is saying `@Override is not allowed when implementing interface method` – J.Olufsen Jan 30 '15 at 23:13
  • 1
    @RCola, if that is the case - remove it ;) Simply make sure that the method has the same signature. This was changed between Java 5 and Java 6 (read more about it [here](http://stackoverflow.com/questions/987973/why-does-eclipse-complain-about-override-on-interface-methods)). Even better, if possible upgrade to Java 6. – wassgren Jan 30 '15 at 23:16
  • This was another of a growing list of misgivings I have about Java, which had been my "go to" language historically. There is no reason to conflate two disparate notions: implementing (fulfilling) an interface and overriding a superclass method, which is what Java 6 did with ``@override`` for interface implementation. I put this in the same illconceived category as the .length vs length() vs size() idioms. –  Jan 31 '15 at 16:59
0

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.

Elliott Frisch
  • 198,278
  • 20
  • 158
  • 249
  • Why is this an "override" issue? He's implementing the method from an interface inside an abstract, not overriding a superclass's method. –  Jan 30 '15 at 22:50
  • 1
    @tgm1024 Because the method in the interface had a different name in the abstract class. Thus the abstract class did not override a super-type (the interface) method. Adding the annotation would have caught that error at compile time. As I said in my answer. – Elliott Frisch Jan 30 '15 at 22:51
  • Using ``@override`` broadcasts his intention to override a method. Had they been named the same, it would still have not overridden anything, correct? –  Jan 30 '15 at 22:55
  • If they had been named the same, the abstract class would have implemented the method (and thus the concrete class would not be required to also override the method). What are you asking? – Elliott Frisch Jan 30 '15 at 22:57
  • Funny. The (at)override is messing with SO's (at)user functionality. –  Jan 30 '15 at 22:57
  • 1
    @tgm1024 Use double backquotes. ``@Override``. – Elliott Frisch Jan 30 '15 at 22:57
  • in that case the abstract would merely be implementing the method. Wouldn't the "(at)override" then complain that nothing was being overridden? –  Jan 30 '15 at 22:58
  • 2
    No. Because the abstract class **can** merely be implementing the method. And the thing being overridden is the interface. – Elliott Frisch Jan 30 '15 at 22:59
  • 1
    I just double checked. ``@override`` does indeed include implementing an interface. You're correct Elliott. The linked javadoc listed above said "Indicates that a method declaration is intended to override a method declaration in a supertype." I didn't think interfaces applied. –  Jan 30 '15 at 23:04
  • From the conversation here: http://stackoverflow.com/questions/14869039/why-there-is-no-implements-annotation-in-java, apparently using ``@override`` for interface implementations was added in Java 6. Frankly, IMO that should have been an ``@implements`` added to the language, but it's already something that croaks quickly if the method is missing. –  Jan 30 '15 at 23:12
0

Well the first thing that I notice is that your method is not named the same thing in the interface and the abstract class.

mmaynar1
  • 326
  • 4
  • 14