I've been testing supertype generics with Java, but I've come to a roadblock. This is the sample code I was testing:
import java.util.*;
class GenericTests {
public static void main( String[] args ) {
List<B> list3 = new ArrayList<B>();
testMethod( list3 );
}
public static void testMethod( List<? super B> list ) {
list.add( new A() );
list.add( new B() );
}
}
class A { }
class B extends A { }
When compiled, the error is:
GenericTests.java:20: error: no suitable method found for add(A)
list.add( new A() );
^
method List.add(int,CAP#1) is not applicable
(actual and formal argument lists differ in length)
method List.add(CAP#1) is not applicable
(actual argument A cannot be converted to CAP#1 by method invocation conve
rsion)
where CAP#1 is a fresh type-variable:
CAP#1 extends Object super: B from capture of ? super B
1 error
I thought that given the lower bound of B, you could add any supertype? Or does this only apply to the references (i.e. arguments within the method signature) because allowing supertypes would break the type checking?