aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Generics problem- why not taking Animal, Object and super of Dog Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Generics problem- why not taking Animal, Object and super of Dog" Watch "Generics problem- why not taking Animal, Object and super of Dog" New topic
Author

Generics problem- why not taking Animal, Object and super of Dog

umesh rawat
Ranch Hand

Joined: Mar 03, 2007
Posts: 41


Umesh rawat <br />Rmw -2019

http://100javaquestions.blogspot.com
umesh rawat
Ranch Hand

Joined: Mar 03, 2007
Posts: 41
someone is there to give the answer ?
Tuna Töre
Ranch Hand

Joined: Aug 17, 2008
Posts: 219

void addAnimal(ArrayList <? super Dog> animals )

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 ]

blog: http://tunatore.wordpress.com
SCJP 6.0 + SCWCD 1.5
Tuna Töre
Ranch Hand

Joined: Aug 17, 2008
Posts: 219

I would like you give an example for you I have done before
It will help you I think

package com.scjp.genericsExamples;

import java.util.ArrayList;
import java.util.List;

public class GenericGeneralTest {

public static void main(String[] args) {


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());

}
}
Tuna Töre
Ranch Hand

Joined: Aug 17, 2008
Posts: 219

To be clear for your question

For this method
You can call this method with

List<Animal> animalList = new ArrayList<Animal>();
canAddWithSuper(animalList);
But you can only add Dog to it.
the other option is

List<Dog> dogList = new ArrayList<Dog>();
canAddWithSuper(dogList);

you can only again add Dog to it !!

? gives you only see the collection content however with super keyword
you can pass animalList to the method and you can see the Collection content!

I think it is clear now

public static void canAddWithSuper(List <? super Dog> list)
{
list.add(new Dog());

// won't work with list.add(new Animal());
}
umesh rawat
Ranch Hand

Joined: Mar 03, 2007
Posts: 41
umesh rawat
Ranch Hand

Joined: Mar 03, 2007
Posts: 41
why this is happening
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9305
    
  17

in case of ArrayList<? super 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....


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
ram potana
Greenhorn

Joined: Mar 15, 2011
Posts: 1

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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Generics problem- why not taking Animal, Object and super of Dog