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.