• 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
  • Paul Clapham
  • Tim Cooke
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Frank Carver
  • Henry Wong
  • Ron McLeod
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Himai Minh

Doubt in generics

 
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Ranch Hand
Posts: 652
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 115
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 447
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Ranch Hand
Posts: 377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi jinesh,

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

 
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks

my doubt is clear.

thnaks for the help.
 
keep an eye out for scorpions and black widows. But the tiny ads are safe.
Garden Master Course kickstarter
https://coderanch.com/t/754577/Garden-Master-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic