Java Generics: Unlimited Substitution Generalization Not Working for Level 2

I think this is a very confusing title, but I don’t know what else to call - probably answered somewhere, but I couldn’t find anything. Take this example:

List<Class<? extends Integer>> myList;
void foo() {
    bar(myList);
}
void bar(List<Class<?>> a) { /* ... */ }

He does not compile (not applicable arguments, he says). If I remove the restriction, it works fine:

List<Class<?>> myList;
void foo() {
    bar(myList);
}
void bar(List<Class<?>> a) { /* ... */ }

Class<? extends Integer>is more specific than Class<?>. Why does it stop working? Please note that this problem occurs only in second-level generics. If there was no list, simple Class<? extends Integer>and Class<?>, it also works. But it seems that it stops working when generics are two hundred and more levels. Any reasons / workarounds / etc?

+4
source share
1

, List<B> List<A>, B A. List<Class<? extends Integer>> List<Class<?>>, Class<? extends Integer> Class<?>. , bar a.add(Object.class), Class<Object> Class<?>.

bar List<? extends Class<?>>.

+4

All Articles