In Effective Java by Joshua Bloch, it says, in reference to overloading:
Exporting multiple overloadings with the same number of parameters is unlikely to confuse programmers if it is always clear which overloading will apply to any given set of actual parameters. This is the case when at least one corresponding formal parameter in each pair of overloadings has a “radically different” type in the two overloadings. Two types are radically different if it is clearly impossible to cast any non-null expression to both types.
However, then it goes on to say:
Array types and class types other than Object are radically different. Also, array types and interface types other than Serializable and Cloneable are radically different.
My question is why are interface types considered radically different, despite Java allowing for multiple interface implementations?
For example, say I have class Foo that implements interface Barable and Bazable. If I had a pair of overloaded methods, one accepting Barable as a parameter, and the other accepting Bazable, why wouldn't that be ambiguous? The following, for example, won't compile due to ambiguous overloading:
interface Bazable {
public void baz();
}
interface Barable {
public void bar();
}
class Foo implements Barable, Bazable {
public Foo() {
}
public void baz() {
System.out.println("Can baz");
}
public void bar(){
System.out.println("Can bar");
}
}
class Main {
public static void main(String[] args) {
Foo f = new Foo();
test(f);
}
public static void test(Barable t) {
System.out.println("Using barable overload");
}
public static void test(Bazable t) {
System.out.println("Using bazable overload");
}
}