Originally posted by Rose Thomas:
wildcard super means accepting generic type that is of the type defined in super tag, or a supertype of type. Nothing lower in inheritance tree can come in.
e.g. List<? super Dog> animal will accept type Dog OR supertype of Dog(say Animal or even Object). But not "fluffy" for eg.
Hi
Rose,
I have the following code:
package chapter7.collections;
import java.util.*;
public class ListGen {
public static void main(
String[] args) {
List<? super String> list = new ArrayList<String>();
list.add("nothing");
list.add(new Object());
Bar b = new Bar();
b.addToList(list);
System.out.println("it all came good");
}
}
class Bar {
public void addToList(List<Object> list){
list.add(new Dog());
}
class Dog{
int bark;
public void eat(){}
}
}
When I try to add the object in the array list I'm getting a compiler error, I'm using eclipse and it says that line is invalid along with the call to bar.addToList() and tried to instantiate the arrarlist as List<? super String> list = new ArrayList<Object>(); and it still gives me errors why I cannot add an object since its a super class of string?... thanks.