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


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Doubt in generics" Watch "Doubt in generics" New topic
Author

Doubt in generics

jinesh parikh
Ranch Hand

Joined: Apr 20, 2007
Posts: 48
I have one question regarding the wildcard in generics.

<? extends ..> when ever we are using the above wildcard we aren't able to
add anything to the corresponding collection.Now my question is why doesn't it allowed to add anything to the underlying collection???

for example......


class Animal{}
class Dog extends Animal{}
class AddtoCollection{

public void addAnimal(List<? extends Animal> animals) {
animals.add(new Dog());
}
public static void main(String args[]){
AddtoCollection obj=new AddtoCollection();
List<Dog> objlst=new ArrayList<Dog>();
obj.addAnimal(objlst);
}

}

it gives me error at the statement
animals.add(new Dog());

my question is why does it is giving me error?
Nik Arora
Ranch Hand

Joined: Apr 26, 2007
Posts: 652
Hi,
You cannot add anything when you are using ? as wildcard.





Regards
Nik
jinesh parikh
Ranch Hand

Joined: Apr 20, 2007
Posts: 48
thanks nik for your reply
now my second question is why it has been made such a way that we can't add
anything when we are using ? as wildcard
Sasha Ruehmkorf
Ranch Hand

Joined: Mar 29, 2007
Posts: 115
If you have a reference variable of type List<? extends Animal> this variable could reference an Object of type List<Cat>. So, I think it's a good thing, that the compiler won't let you put dogs in it.
anil kumar
Ranch Hand

Joined: Feb 23, 2007
Posts: 447
Hi

jinesh

To iterate the elements in the List,Map....

class Animal
{
}
class Dog extends Animal
{
}
class cat extends Animal
{
}


some method like this

public void display(List<Animal>l)
{
}

If you want to iterate ,then you should or you must send a List of type Animal means List<Animal>

We can send any other thing like List<Dog>,List<Cat>.

If you define a method say List<? extends Animal>.

Menas we can send List<dog>,because Dog is-A Animal.
We can even send List<Cat> ,because Cat is- A Animal


Thanks

Anil Kumar
[ June 13, 2007: Message edited by: anil kumar ]
Manfred Klug
Ranch Hand

Joined: Jun 04, 2007
Posts: 377
Hi jinesh,

since the compiler can't determine that you don't do something like this:

Louis Moloney
Ranch Hand

Joined: Feb 06, 2007
Posts: 59
hey guys,

i noticed some of you are under the impression that you can't add to a type collection when using ? wildcard. you can if using super but not extends.

you cannot add to this: List<? extends Number>

you CAN add to this: List<? super Number>


Nik Arora
Ranch Hand

Joined: Apr 26, 2007
Posts: 652
Hi,
now my second question is why it has been made such a way that we can't add
anything when we are using ? as wildcard


I hope your doubt is cleared by the explanation given by all of them. If you have doubt post it here.

Regards
Nik
jinesh parikh
Ranch Hand

Joined: Apr 20, 2007
Posts: 48
thanks

my doubt is clear.

thnaks for the help.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Doubt in generics
 
Similar Threads
Generics Question
Generics Problem
generics
Generics
Generics compendium