• 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

difference between List<? super Dog> animals and List<? extends Animal> animals

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


why this is OK and



why this is not?

 
Ranch Hand
Posts: 35
Tomcat Server Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When you are using wildcard List<? super Dog> it means you can pass any type that is Dog or superclass of Dog and we can add anything that is of type Dog or any subclass of Dog, this happens because as we know in polymorphism a parent type reference can hold its subclass object type.For example
(1).addAnimal(new ArrayList<Dog>());//method call or
(2).addAnimal(new ArrayList<Animal>());//method call
(3).public void addAnimal(List<? super Dog> animals) { }//called method
In the above calls the called method guirantees that adding anything that is Dog or subclass of Dog is acceptable weather you pass it (1) or (2) to (3).

But while using wildcard List<? extends Animal> you can pass it any List that is of generic type that is of type Animal or subtype of Animal.This syntax is used because we are telling the compiler that we are only using the List just for reading purpose and we will not add anything to it, if added the compiler will issue a compilation error.Error is given by compiler because imagine if addition was allowed in this syntax we would have added a wrong type into a wrong list since we could have passed it ArrayList<Dog> and we were free to add a Cat object into it and then at some point in runtime it would have given a Runtime Exception.
The <? extends Type> is meant to just use it for reading purpose and nothing else.
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Rishi Dhar wrote:
But while using wildcard List<? extends Animal> you can pass it any List that is of generic type that is of type Animal or subtype of Animal.This syntax is used because we are telling the compiler that we are only using the List just for reading purpose and we will not add anything to it, if added the compiler will issue a compilation error.Error is given by compiler because imagine if addition was allowed in this syntax we would have added a wrong type into a wrong list since we could have passed it ArrayList<Dog> and we were free to add a Cat object into it and then at some point in runtime it would have given a Runtime Exception.
The <? extends Type> is meant to just use it for reading purpose and nothing else.




It is probably easier to envision, if you understood why...

A List<? extends Animal> reference type variable points to a List of a generic type that is an Animal or extends Animal. So, the reference variable can point to a List<Animal>, List<Cat>, List<Dog>, or even a list to a generic type of Animal that hasn't been coded yet. And strangely, the compiler doesn't know what generic type it is from the reference type.

So, the reason you can't add a Cat instance is because you can't add a Cat instance to a List<Dog>. The reason you can't add a Dog instance is because you can't add a Dog instance to a List<Cat>. And since the compiler doesn't actually know the type of the generic, it will only allow you to add something that will work for all generic types that is an, or extends, Animal. So, the only thing that you can add is null.

Henry
 
Sandeep Mandge
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you so much got it
 
Danger, 10,000 volts, very electic .... tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic