This is not allowed. Why not?
When methods are called in Java, the method with the most specific type signiture is used.
In the example:
interface A {...}
interface B {...}
class X extends A, B {...}
void f(X);
void f(A);
void f(B);
X x = ...
f(x); // calls f(X)
f((A)x); // calls f(A)
f((B)x); // calls f(B)
We all know how to cast to call the correct method we want.
But in the example
<T extends X> void g(T); //G1
<T extends A & B> void g(T); //G2
g(x); // calls G1
We can call G1
, but there is no way to call G2
without redirection.
We cannot downcast to A & B
, only to either A
or B
.
The A & B
does not seem well intgrated into the type system (most people won't even know of it), therefore Java does not see A
as being a different type from A & B
when choosing which version of a method to dispatch to.
Futher examples of madness:
interface A
interface B extends A
<T extends A & B> void f(T)
<T extends B> void f(T)
This will compile, but you can't call either of them
B b = new B(){...}
f(B); // method reference is ambiguious.