Here’s my issue: given these classes
class A {}
class B extends A {}
This code compiles:
List<Class<? extends A>> list = Arrays.asList(B.class, A.class);
And this does not:
List<Class<? extends A>> anotherList = Arrays.asList(B.class);
What gives?
UPDATE: This code compiles in Java 8. Apparently, due to ‘Improved Type Inference’.
In the first example, the inferred type of the
Arrays.asList()call isList<Class<? extends A>>, which is obviously assignable to a variable of the same type.In the second example, the type of the right side is
List<Class<B>>. WhileClass<B>is assignable toClass<? extends A>,List<Class<B>>is not assignable toList<Class<? extends A>>. It would be assignable toList<? extends Class<? extends A>>.The reason for this is the same one as why a
List<B>isn’t assignable toList<A>. If it was, it would make the following (not-typesafe) code possible: