This week's book giveaway is in the Clojure forum.
We're giving away four copies of Clojure in Action and have Amit Rathore and Francis Avila on-line!
See this thread for details.
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

generics

 
sweety singh
Ranch Hand
Posts: 49
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi all... can somebody tell me the difference between <? extends Animal> and <? super Animal> with example.... i am slightly confused...
thanks in advance...
 
Daniel Del Moral
Ranch Hand
Posts: 32
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There two mayor difference. The wildcard (?) is used to say "any class", so <? extends Animal> would something like "any class that extends Animal" and <? super Animal> would be "any class that is a superclass of Animal".

For example, let's suppose that Dog extends Animal and Cat extends Animal. And we know that Animal extends Object (implicitly).

So, we could do this:

List<? extends Animal> list1 = new ArrayList<Dog>(); (it's OK, Dog extends Animal)
List<? extend Animal> list2 = new ArrayList<Cat>(); (it's OK, Cat extends Animal)
List<? extends Dog> list3 = new ArrayList<Animal>(); (NOT OK)
List<? super Dog> list4 = new ArrayList<Animal>(); (it's OK, Animal is a superclass of Dog)

Notice that wildcards can ONLY by used on declaration. These is absurd:
List<? extends Animal> list1 = new ArrayList<? extends Animal>(); (Horrible wrong)

So, why is there that difference? Well, if you declare a class with a generic using the wildcard that extends another class, you can't ADD anything to it. This will result as a big compile error:

List<? extends Animal> list1 = new ArrayList<Dog>();
list1.add(new Dog()); (Compile error)

But, you can add to a instance declare with the wildcard that supers another class:

List<? super Dog> list1 = new ArrayList<Animal>();
list1.add(new Dog()); (It's good)

I hope this clears out your question.
 
sweety singh
Ranch Hand
Posts: 49
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hey thanks for that reply... but i want to ask you one thing... in the following code..

Code:

public void addAnimal(List<? extends Animal> animals)

{
animals.add(new Dog()); //1
}

in line 1.. the book says "No! can't add if we use <? extends Animal>"
why is it so...
[ May 25, 2008: Message edited by: sweety singh ]
[ May 25, 2008: Message edited by: sweety singh ]
 
Daniel Del Moral
Ranch Hand
Posts: 32
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can't add to prevent adding the wrong object to the list. For example:

public void addAnimal(List<? extends Animal> animals)
{
animals.add(new Dog()); //1
}

Imagine for a minute that that compiles fine. What happens if I called this method like this:

addAnimal(new ArrayList<Cat>());

Cat extends Animal, so it would be fine, but you did you noticed that I added a Dog? So the adding restriction is there to prevent this circumstances.
 
sweety singh
Ranch Hand
Posts: 49
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
but dog also extends animal.... i am confused... i did not understand..
 
Deepak Bala
Bartender
Posts: 6663
5
Firefox Browser Linux MyEclipse IDE
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by sweety singh:
but dog also extends animal.... i am confused... i did not understand..


Yes it does. The point is that you cannot be sure what will be added if the code were allowed to be compiled. Just remember that when you see the ? symbol you cannot add anything to the data structure
 
Santosh Arun Kumar Gudipati
Greenhorn
Posts: 8
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So you say that any class that uses generic wild card declaration cannot able to add any thing??
even in the case of it extends another class or it superclass of another??
 
sweety singh
Ranch Hand
Posts: 49
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hey thanks all... i have understood...
 
Aditya Jha
Ranch Hand
Posts: 227
Eclipse IDE Java Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,


Just remember that when you see the ? symbol you cannot add anything to the data structure



So you say that any class that uses generic wild card declaration cannot able to add any thing??
even in the case of it extends another class or it superclass of another??


Can't really agree on that. As Daniel Del Moral explained in his post,

is perfectly fine.

Best regards,

- Aditya
 
Deepak Bala
Bartender
Posts: 6663
5
Firefox Browser Linux MyEclipse IDE
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can't really agree on that


I was commenting on the extends scenario. I guess I forgot to mention that
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic