Search code examples
javainterfaceabstract-class

Child interface with same method than parent


If interface B extends interface A, and provides a same method as interface A does that mean that method ovewrite the other method?

Edit: changed the word implements to extend since that is wrong and actually didn't mean to write implement hehe...

The purpose of this example code that I found on my exam was to know how the extending between interfaces work and what was asked here was to provide the body of the ConcreteClass, specifically what methods must the class implement: the solution is in the body of the class but what I don't understand is what happens to the methodY(String z) in the Interface_Y, does it get overwriten by methodX(String x) in Interface_Z, shouldn't methodY(String z) be also on the list of the methods that ConcreteClass must implement or does the name of the method not matter when it comes to the possibility of being overwritten?

interface Interface_X{
    public int methodX(int x);
}

interface Interface_Y extends Interface_X{
    public void methodY(String z);
    public int methodX(int y);
}

interface Interface_Z extends Interface_X{
    public void methodX(String x);
    public int methodX(int y);
}

abstract class AbstractClass implements Interface_Z, Runnable{
    public abstract int methodX();
    public int methodX(int c) {
        return 0;    
    } 
}

public class ConcreteClass extends AbstractClass{

    @Override
    public void run(){

    }

    @Override
    public int methodX(){
        return 0;
    }

    @Override
    public void methodX(String z){

    }
}

Solution

  • but what I don't understand is what happens to the methodY(String z)

    Nothing happens to it because Interface_Y where that method belongs is not in the type hierarchy of ConcreteClass.

            Interface_X 
           /           \             Runnable
    Interface_Y   Interface_Z           /
                         \             /
                    AbstractClass     /
                           \         /
                          ConcreteClass