0

Let's say I have two interfaces InterfaceA and InterfaceB with the same method signature. InterfaceA:

public interface InterfaceA {
    void printInterfaceName();
}

And InterfaceB:

public interface InterfaceB {
    void printInterfaceName();
}

And I have a Printing class that is implementing both the interface Printing class:

public class Printing implements InterfaceA, InterfaceB {
    // want to override the printInterfaceName() method such way
    // so that it will give an output which given in main method comment
}

And I have a Main class with a main method on it

public class Main {
    public static void main(String[] args) {
        InterfaceA a = new Printing();
        InterfaceB b = new Printing();

        a.printInterfaceName(); // this is should print `INTERFACE-A`
        b.printInterfaceName(); // and this is should print `INTERFACE-B`
    }
}

1 Answers1

1

Since the method is not aware of the context (a.printInterfaceName() or b.printInterfaceName()), it will not be possible to distinguish between both of them and print INTERFACE-A or INTERFACE-B.

As far as I know, neither reflection nor instanceof can do the trick. Having default Methods in the Interfaces wont work either.

Maybe you can write more about your intention, so that a different solution can be suggested?

redPanda
  • 76
  • 5