简体   繁体   中英

Can a class inherit implementation of a method contruct imposed by an interface from other class?

I am looking at an old code written by some else and I come to see that there is a class implementing an Interface but not implementing all methods declared in the interface.

I found out this when I try to look for implementation of a method in the interface using eclipse (highlight the method and do ctl+Shit+G = no implementation is found in the work space). studying the code closely I found out that the implementation of the method is actually inherited from another Abstract class that the class extends.

here is a simple version of what I am talking about:

interface myInterface{
    int needsTobeImplemeted(int x);
}

abstract class myAbstractClass{
    int needsTobeImplemeted(int x){
        return x;
    }
}

class myClass extends myAbstractClass implements myInterface
{

}

calling the method from main prints => result: 0

public static void main(String[] args) {
    myInterface interfaceType = new myClass();
    System.out.println("result: " + interfaceType.needToBeImplemeted(0));
}

and my question is how is this possible? what will happen if myClass overide the method:

@override 
int needsTobeImplemeted(int x){
    return x*x;
}

which method it is implementing now, the on in the interface or the one in Abstract class? by the way the extended class should not necessary be an abstract class, it could be a normal class; it is enough if it has a method with same signature as the one defined in myInterface.

any explanation would be appreciated ;)

overriding the method in myClass :

public class myClass extends myAbstractClass implements myInterface {

  @Override
  public int needToBeImplemeted(int x)
  {
    return x*x;
  }
}


public static void main(String[] args) {
    myClass classType = new myClass();
    myAbstractClass abstractType = new myClass();
    myInterface interfaceType = new myClass();

    System.out.println("result: " + classType.needToBeImplemeted(2));
    System.out.println("result: " + abstractType.needToBeImplemeted(2));
    System.out.println("result: " + interfaceType.needToBeImplemeted(2));
}

Result:

result: 4

result: 4

result: 4

Which method it is implementing now, the on in the interface or the one in Abstract class?

The class is not "implementing" a method at all. It "implements" an interface. And implementing an interface just means, "having all the methods described by that interface".

It doesn't matter where in the class or its inhertiance tree that method was defined. And therefor it is no difference if you intended to implement the method found in the interface or in the abstract method. All that counts is if a method with that signature exists.

A non-abstract class that implements an interface, must have an implementation of all the interface's methods. It doesn't matter if it implements those methods or inherits the implementation from its super-classes. If it overrides the needsTobeImplemeted implementation of the super-class, this means that calling needsTobeImplemeted on an instance of this class will invoke the implementation of the sub-class.

In your example, interfaceType.needToBeImplemeted(0) returns 0 no matter if you override it in the myClass sub-class, since 0 = 0*0.

myClass implements myInterface which means that it has to have a behaviour described by myInterface . And myClass has this behaviour because it is a myAbstractClass and inherits all it's methods .

Answering your question:

which method it is implementing now, the on in the interface or the one in Abstract class?

myClass is not implementing the method itself, it inherits the method from myAbstractClass to comply to myInterface .

If you put this:

@Override 
int needsTobeImplemeted(int x){
    return x*x;
}

inside myClass you would be overriding the implementation of myAbstractClass .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM