File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
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


JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Generics" Watch "Generics" New topic
Author

Generics

Balaji Bang
Ranch Hand

Joined: Apr 23, 2007
Posts: 180
HI

what is the meaning of below statement ??? we Cann't add anything to this list.

List<? extends Animal> dog=new ArrayList<Dog>(); //should not add anything to list

but I tried adding null.

dog.add(null);

but I am able to add null to this dog.
Can anybody explain me, why we are able to add null...

2) List<? super Dog> dog=new ArrayList<Dog>();
dog.add(new Animal()); // compiler error
Why this gives compiler error
Thanks.
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9303
    
  17

when you write

List<? extends Animal>

you are saying that this is a list of animals. It could be dog, cat or anything else. But you don't know what it actually is. So you can't add anything to this list. What you actually know is that the list contain animals. So you can retrieve elements from this list but you can't add any elements.

But when you say

List<? super Dog>

Here you are saying that this list is of a super type of Dog. It can be a direct super class like Animal or indirect super class like Object or it can be of Dog itself. So you are basically not sure. But one thing's for sure. The list is of a super type of Dog or Dog itself. So you can add elements of type dog or sub class of dog to the list. But you can't add animal objects to the list as the list might be of type Dog too and every animal is not a dog. Just see this

List<? super Dog> list = new ArrayList<Dog>();
list.add(new Animal()); //1, not allowed

If 1 was allowed, then we would have added an animal to a dog list. But an animal is not a dog. That's why it is not allowed...

[Edit: banu added one more question by editing his post )


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Balaji Bang
Ranch Hand

Joined: Apr 23, 2007
Posts: 180
But I am able to add null to this.

List<? super Dog> dog=new ArrayList<Dog>();
dog.add(new Animal());

Why this is not allowing to add Animal which is super type for Dog..
ramesh maredu
Ranch Hand

Joined: Mar 15, 2008
Posts: 210

List<? super Dog> dog=new ArrayList<Dog>();
dog.add(new Animal());

Why this is not allowing to add Animal which is super type for Dog..


dog can point to any List which can hold Dog or its Super types. assume that at runtime you have received a list of Dog objects, If you are allowed to add Animal to the list you will end up with Animal object in Dog list

SCJP 1.5 94%.
The greatest glory in living lies not in never falling, but in rising every time we fall.
Ruben Soto
Ranch Hand

Joined: Dec 16, 2008
Posts: 1032
I think Ankit with his excellent response and Ramesh must have cleared your previous doubts.

As to why you can add null:
You can add null because you can assign null to a reference of any type. To simplify things a little, you can consider null to be a value of any reference type, so you can assign null to Integer, to Animal, to Object, etc. When you add null you will be adding a null reference of whatever type the collection actually is. No matter what that type is, you can add a null reference to the collection.


All code in my posts, unless a source is explicitly mentioned, is my own.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Generics