Idealized scenario
Let's imagine a scenario, with 2 classes like A
and B
where B extends A.
I also have a third generic class C<T>
with a method m(T t)
.
If I create a object C<C<A>> container
and another object C<B> obj = new C<>();
why can't I run container.m(obj)
?
Full code example
public class Test {
class A {}
class B extends A {}
class C<T> {
void m(T t) {}
}
void test() {
C<C<A>> container = new C<>();
C<B> obj = new C<>();
container.m(obj);
}
}
Practical case
This Issue becomes apparent with nested lists where the C<A>
in my simplified example would be a a List<A>
.
Oracle highlights some limitations of generics, but I can't find anything describing this issue and how to deal with it.