Why isn’t this working, and how do I add an element to the collection?
import java.util.ArrayList;
import java.util.Collection;
public class CollectionTest {
public void main(String[] args) {
Collection<? extends A> list = new ArrayList<B>();
B b = new B();
list.add(b); // FAIL
}
public class A {
}
public class B extends A {
}
}
[quote]The method add(capture#1-of ? extends CollectionTest.A) in the type Collection<capture#1-of ? extends CollectionTest.A> is not applicable for the arguments (CollectionTest.B)
[/quote]
And why can I assign it ArrayList but not actually put in any Bs :
I assume its a type-erasure problem, but I dont know what the fix IS.