This week's book giveaway is in the OCAJP forum.
We're giving away four copies of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) and have Khalid A Mughal & Rolf W Rasmussen on-line!
See this thread for details.
Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Doubt in generics

 
jinesh parikh
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 652
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
You cannot add anything when you are using ? as wildcard.





Regards
Nik
 
jinesh parikh
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 115
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 447
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 377
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi jinesh,

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

 
Louis Moloney
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 652
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 51
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks

my doubt is clear.

thnaks for the help.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic