What's the purpuse behind wildcards and how are they different from generics?

Posted by Nazgulled on Stack Overflow See other posts from Stack Overflow or by Nazgulled
Published on 2010-05-27T16:13:22Z Indexed on 2010/05/27 16:21 UTC
Read the original article Hit count: 184

Filed under:
|
|

Hi,

I never heard about wildcars until a few days ago and after reading my teacher's Java book, I'm still not sure about what's it for and wwhy would I need to use it.

Let's say I have a super class Animal and few sub classes like Dog, Cat, Parrot, etc... Now I need to have a list of animals, my first thought would be something like:

List<Animal> listAnimals

Instead, my colleagues are recommending something like:

List<? extends Animal> listAnimals

Why should I use wildcards instead of simple generics?

Let's say I need to have a get/set method, should I use the former or the later? How are they so different?

© Stack Overflow or respective owner

Related posts about java

Related posts about generics