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.

3
  • Does it meter at all? Commented Apr 5, 2013 at 13:55
  • (In Java SE 8 there will be some rules for overriding default method implementations.) Commented Apr 5, 2013 at 14:03
  • @partlov, it definitely does matter if one interface is supposed to show an elephant and another interface should show a window on screen. That's why C#, for example, provides a way to separate implementations in such cases. Commented Apr 5, 2013 at 14:16

4 Answers 4

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.

Sign up to request clarification or add additional context in comments.

1 Comment

This explanation is awesome.
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.

Comments

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.

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.