wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes generics Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "generics" Watch "generics" New topic
Author

generics

sweety singh
Ranch Hand

Joined: Dec 15, 2007
Posts: 49
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

Joined: May 24, 2008
Posts: 32
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.


SCJP 5, SCWCD 5
sweety singh
Ranch Hand

Joined: Dec 15, 2007
Posts: 49
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

Joined: May 24, 2008
Posts: 32
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

Joined: Dec 15, 2007
Posts: 49
but dog also extends animal.... i am confused... i did not understand..
Deepak Bala
Bartender

Joined: Feb 24, 2006
Posts: 6661
    
    5

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


SCJP 6 articles - SCJP 5/6 mock exams - More SCJP Mocks
Santosh Arun Kumar Gudipati
Greenhorn

Joined: May 25, 2008
Posts: 8
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

Joined: Dec 15, 2007
Posts: 49
hey thanks all... i have understood...
Aditya Jha
Ranch Hand

Joined: Aug 25, 2003
Posts: 227

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

Joined: Feb 24, 2006
Posts: 6661
    
    5

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
 
subject: generics
 
Similar Threads
ArrayList and List
Generics...please clarify
inheritence
Help me fix this,so I can compile(generics doubt)
<? super Integer>