• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Generics

 
Ranch Hand
Posts: 182
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Sheriff
Posts: 9708
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 )
 
Balaji Bang
Ranch Hand
Posts: 182
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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..
 
Ranch Hand
Posts: 210
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Ranch Hand
Posts: 1032
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic