1

Java is said not to support multiple inheritance but one can implement more than one interface. So in the case of the snippet below which of the show() method would be implemented.

public interface I1{

   public void show();

}


public interface I2{

   public void show();

}


class mine implements I1, I2{

    @override
    public void show(){

       //do something

    }
}

My question is how do i know the show() method that was overridden.

blank
  • 17,852
  • 20
  • 105
  • 159
Ayodeji
  • 579
  • 2
  • 8
  • 25

4 Answers4

3

My question is how do i know the show() method that was overridden.

You can not distinguish this. There is only one implementation of the show() method, which can be called through either interface.

Andreas Fester
  • 36,091
  • 7
  • 95
  • 123
3

Java doesn't have an equivalent to C#'s public void A.show(), cause it simply doesn't let the interfaces conflict in that way. If you implement two interfaces that declare a method with the same name and the same argument types, they either also have the same return type, or Java will not even compile the code. And once the return type is also the same, a class that implements one or the other actually implements both simultaneously, as the signature satisfies the requirements of both interfaces.

Of course, if you want to verify...

public class Example {
    interface A { public void show(); }
    interface B { public void show(); }

    class C implements A, B {
        public void show() { System.out.println("show()ing"); }
    }

    public static void main(String[] args) {
        C c = new C();

        A a = c;
        a.show();  // does something, trust me :P

        B b = c;
        b.show();  // does something too
    }
}

C's void show() satisfies both interfaces' method declarations, so all's well, and two lines appear in the console (one for the call through an A reference, one for the B call).

Now, say you had

interface A { public void show(); }
interface B { public int show(); }  <-- different return type!

class C implements A, B { public void show(); }

It's somewhat obvious which method you're trying to implement. This wouldn't work, though -- and can't, because Java won't let the two interfaces' methods exist in the same class.

Now, one last example.

interface A { public void show(); }
interface B { public int show(int); }  <-- different return type!

class C implements A, B {
    public void show() { System.out.println("Showing..."); }
    public int show(int i) { System.out.println(i); }
}

This is just fine. Since the two interfaces declarations only share a name and not arg types, Java lets them coexist in C. But it also requires that you implement one method for each interface.

cHao
  • 84,970
  • 20
  • 145
  • 172
2

As interface doesn't have method definitions. it will not matter which interface's show method you are overriding.

Implemeting 2 interfaces in a class with same method.Which interface method is overridden?

If a type implements two interfaces, and each interface define a method that has identical signature, then in effect there is only one method, and they are not distinguishable. If, say, the two methods have conflicting return types, then it will be a compilation error. This is the general rule of inheritance, method overriding, hiding, and declarations, and applies also to possible conflicts not only between 2 inherited interface methods, but also an interface and a super class method, or even just conflicts due to type erasure of generics.

Community
  • 1
  • 1
Achintya Jha
  • 12,735
  • 2
  • 27
  • 39
0

That's fine. Just think of those two interfaces happen to have same method declaration.

IndoKnight
  • 1,846
  • 1
  • 21
  • 29