First of all in generics there are no relation like polymorphism I mean You cannot say
List<Animal> list = new ArrayList<Dog>; //this is WRONG generics are different than arrays. In arrays you can use polymorhism.
Son when you declare
List<Dog> list = new ArrayList<Dog>; This means you can only put dog to it no animal or any other stuff. Otherwise you get error!
For your question
If you are calling a method with generic syntax for example
void addAnimal(List<Dog> animals )// you can only pass List<Dog> type here, you cannot pass List<Animal> here !!!
? question mark allows generic method calls with inheritance but it does not allow you to modify the generic list but you can only add Dog (the object in the syntax) and you can use get methods on the collection but you cannot modify the collection so you cannot add any to it other than Dog This is the rule!!
public void addAnimal(ArrayList <?> animals )
for this It can take any List but I said before you cannot add it anything You can only see the content of the collection.
You can find more informatin in K&B book it really express generics good way you can read more than once , sure you will get the idea
[ October 14, 2008: Message edited by: Anut Walidera ]
List <String> stringList = new ArrayList<String>(); stringList.add("string1"); //won't work stringList.add(3); List animals = new ArrayList(); animals.add(new Dog()); List<Animal> animalList = new ArrayList<Animal>(); animalList.add(new Dog()); animalList.add(new Cat()); //won't work animalList.add(new Human()); animalList.add(new Animal()); //won't work List<Object> objectList = new ArrayList<Integer>(); //won't work List <Animal> animalPolyMorphism = new ArrayList<Dog>(); Animal[] animalArray = new Animal[5]; Animal[] animalArrayref = new Dog[5]; //this works for arrays not for generics
Animal a = animalList.get(0); // no cast required Animal b = (Animal)animals.get(0); // cast is required
legacyMethod(animalList); genericMethodTest(animalList); List<Dog> dogList = new ArrayList<Dog>(); // this won't work subtypes are not work with generic method calls // genericMethodTest(dogList); genericMethodTest(animals); genericMethodTestForDog(dogList); genericMethodTestForDog(animals); canAcceptSubtype(dogList); canAcceptSubtype(animalList); canAddWithSuper(dogList); canAddWithSuper(animalList); wildCard(dogList); wildCard(animalList); //won't work objectList(dogList); List<Object> oList = new ArrayList<Object>(); objectList(oList);
//syntax //? can be in reference part (left) List<?> reference1 = new ArrayList<Dog>(); List<? extends Animal> reference2 = new ArrayList<Animal>(); List<? super Dog> reference3 = new ArrayList<Animal>(); List<? super Integer> list = new ArrayList <Object> (); } public static void legacyMethod(List legacy) { //this method works legacy.add("string"); } public static void genericMethodTest(List<Animal> inAnimalList) { inAnimalList.add(new Dog()); } public static void genericMethodTestForDog(List<Dog> inDogList) { inDogList.add(new Dog()); } //? provides polymorphism for generics public static void canAcceptSubtype(List< ? extends Animal> list) { list.add(null); //thats means you cannot add something Animal a = list.get(0); // you only use get } public static void canAddWithSuper(List <? super Dog> list) { list.add(new Dog()); // won't work with animal list.add(new Animal()); } public static void wildCard(List<?> list) { // won't add list.add(""); //YOU COULD PUT THE WRONG TYPE IN A COLLECTION } public static void objectList (List<Object> oList) { oList.add(new Dog());
the compiler doesn't allow you to add objects of Animal class as you can pass an ArrayList<Dog> to this method. So you cannot store Animal object into a Dog List....
Uses of non-specific type variables are replaced by the upper bound of the type variable.
That is, <? super Dog> is converted to <Dog> during type erasure. So, it does not allow either Animal or Object.
<? super Dog> is converted to <Dog> in the first step.
In the second step, <Dog> is completely removed, because of type erasure.
Post by:autobot
That new kid is a freak. Show him this tiny ad:
a bit of art, as a gift, that will fit in a stocking