Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
umesh rawat
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
umesh rawat
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
someone is there to give the answer ?
 
Tuna Töre
Ranch Hand
Posts: 220
Eclipse IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Tuna Töre
Ranch Hand
Posts: 220
Eclipse IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 220
Eclipse IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
umesh rawat
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
why this is happening
 
Ankit Garg
Sheriff
Posts: 9521
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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....
 
ram potana
Greenhorn
Posts: 1
Hibernate Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 Reply
  • Bookmark Topic Watch Topic
  • New Topic