• 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

Problem in generics

 
Ranch Hand
Posts: 141
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
I am not able to understand why I am not able to add instance of Top class to an arrayslist declared as List <? super Middle>

Please see the following code




Thanks
Paras
 
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In a type safe Middle collection you cannot add a Top.Thats all.
 
Paras Jain
Ranch Hand
Posts: 141
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
But I have declard the Collection type as
List <? super Middle>

not

List <Middle>
 
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you have a collection declared as List<? super X> where X is a certain class, you are allowed to add to the collection only objects that pass IS-A X relationship.
 
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The problem is as the other guys said - You've declared an ArrayList of Middles and are trying to add a object type of Top to it, which does fit the IS-A structure i.e. Top is not a Middle - though Middle is a Top so if you were to change your code to



it would compile.
 
Ranch Hand
Posts: 163
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Jae Stryker:
The problem is as the other guys said - You've declared an ArrayList of Middles and are trying to add a object type of Top to it, which does fit the IS-A structure i.e. Top is not a Middle - though Middle is a Top so if you were to change your code to



it would compile.



This question often causes me confusion.
Here is an extract that i found out in a website.

So even though "? super T" is a lower bound,it depends on final instatntiation of reference
Ex:

List <? super Top> list = new ArrayList <Top> ();

Even though the above code says,List can take any objects of Top anmd Super types,you can only add objects of Top and Subtypes..

Am i right?
Can anyone explain it bit more

Many Thanks
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic