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

Question about wildcards (Boyarsky & Selikoff)

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

I don't understand why the following code doesn't compile:



B is a supertype of C & the list is instantiated as an ArrayList of type B. So why can't I add a B object to the list?

Thank you in advance,

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

 
Ranch Hand
Posts: 491
23
Eclipse IDE Firefox Browser Spring VI Editor AngularJS Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Generics is applied in java for the compile time type safety.during your code compilation compiler knows that list has a type List<? super C>.the captured types for"? super C" is "C" itself,class Object,class B and the class A.so for the compiler List<? super B> can be List<Object>,List<A>,List<B>,List<C>.so these capturing will make a compiler to think that List can have any of these types.during a time you are trying to add instance of class B to the list,compiler is simply thinking that list can be List<C>,List of C,and object of class B does not hold is a relationship with class C.
B is ais not a C.
you may be thinking that the parametric type of object on right hand side:
"List<? super C> list = new ArrayList<B>()" will help the compiler to infer that List is a list of B.but it will do no favor to compiler because object will be created during a runtime and during run time the generic information is not available(deleted after compilation).
so compiler can only infer the type from the type of the reference variable.indeed you can only add instance of C  or null to this list.so make it a point that List<? super E> is a consumer of "E" type instances.

Hope it helps!

Kind regards,
praveen.
 
Sheriff
Posts: 9692
42
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Since <? super C> is open ended, the compiler doesn't know what type of elements the List actually contains as all of the below are valid:


After the declaration the compiler only cares about the declared type of the field, so it can't allow you to add anything as the compiler doesn't know the actual type of the List

 
Helene Shaikh
Greenhorn
Posts: 21
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you for the help! I understand everything now.
 
This is my favorite show. And this is my favorite tiny ad:
Free, earth friendly heat - from the CodeRanch trailboss
https://www.kickstarter.com/projects/paulwheaton/free-heat
reply
    Bookmark Topic Watch Topic
  • New Topic