1

I have below 2 interfaces.

public interface I1 {
    
    public void show();

}

Another one

public interface I2 {
    
    public void show();

}

We have a class which is implementing both.

public class Main implements I1,I2 {

@Override
public void show() {
    
    System.out.println("Hello I am mahima");
}

I ran the program , there is no compilation or run time error. How can I find which interface method is getting called here? Is there anyway to find it?

This question was asked in an Amazon interview first round.

Mark Rotteveel
  • 90,369
  • 161
  • 124
  • 175
Mahima
  • 90
  • 2
  • 17

1 Answers1

7

Mu. That one method is the implementation for both interfaces. That show you wrote cannot be said to be 'I1's show' vs. 'I2's show'. It's both:

I1 i = new Main();
i.show(); // works, prints mahima
I2 j = new Main();
j.show(); // works, prints mahima
rzwitserloot
  • 65,603
  • 5
  • 38
  • 52
  • ya it is printing same output. But is there anyway to distinguish that, if same method is in 2 interface and i am implementing both so i can decide which one i want – Mahima Jan 28 '21 at 21:22
  • 3
    @Mahima both signatures are identical, so the decision which you want is meaningless – peer Jan 28 '21 at 21:24
  • 1
    @Mahima I have for you a red balloon or a red balloon. Which one do you want? It's a trick question. – rzwitserloot Jan 28 '21 at 22:27
  • :) :).... I got is @rzwitserloot . thank you so much. – Mahima Jan 29 '21 at 05:18