• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Paul Clapham
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Roland Mueller
  • Piet Souris
Bartenders:

generics

 
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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...
 
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
but dog also extends animal.... i am confused... i did not understand..
 
Bartender
Posts: 6663
5
MyEclipse IDE Firefox Browser Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hey thanks all... i have understood...
 
Ranch Hand
Posts: 227
Eclipse IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
MyEclipse IDE Firefox Browser Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
reply
    Bookmark Topic Watch Topic
  • New Topic