javagenericstype-bounds

Why doesn't the compiler infer T(interface) to be class that implements T when having double bounded generic type?


I have a class CBound that implements two interfaces IBound1 and Ibound2:

class CBound implements IBound1, IBound2 {}

I have a generic method that accept as type parameter a class type that implements both IBound1 and Ibound2 interfaces:

 public static <T extends IBound1 & IBound2> void getData(T list) {
      //Some code...
}

I made an object of type IBound1 with the imlementation of CBound class:

 IBound1 a = new CBound();
 getData(a); // doesn't work

Why the obj a is not working as a parameter for getData() ?

When I change the code with :

CBound b = new CBound();
getData(b); // It works fine

Solution

  • The compiler has to infer a T that satisfies the bounds at compile time.

    When you pass in a, all that the compiler knows is that a is of type IBound1. Since the method's parameter is also T, the compiler only really have two choices of T here - IBound1 or Object - neither of which satisfy the bounds.

    You might ask, "why doesn't the compiler infer T to be CBound then?" Well there isn't anything in the expression getData(a) that is of type CBound. Even though we know that a actually refers to an object of type CBound by reading the previous lines of code, the compiler doesn't consider that when inferring T. And even if T were inferred to be CBound, you would not be able to pass a to a parameter of type CBound.

    In the case of b however, the compiler knows very well that b is of type CBound (because you have declared it so), so it can successfully infer T to be CBound.