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

Generics ? super Doubt

 
Ranch Hand
Posts: 111
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi There,

I have a doubt in the following code:
The class hierarchy is
Species
^
Animals
^
Dog





I feel that Dog is not a super of Animals. But, there is no error for the line #2 in adding. for the above syntax I thought I can add only "Animals","Species","Object" in the dList. But, I am getting an compiler error saying "cannot instantiate the type".
Can any of you please explain?



Thanks in Advance
Saritha

[ October 06, 2008: Message edited by: saritha babu ]
[ October 06, 2008: Message edited by: saritha babu ]
 
Ranch Hand
Posts: 178
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Saritha,

The how's and why's of your question is discussed here:

https://coderanch.com/t/270553/java-programmer-SCJP/certification/Generics-please-clarify
 
Ranch Hand
Posts: 320
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
List<? super Animals> dlist

declares a reference variable that can hold a List of any type that is Animal OR a superclass to Animal. So your statement is correct that this could be a List<Animal>, List<Species>, or a List<Object>. When it comes to adding, since the generic type of list will hold Animal or higher, the compiler knows that it will hold anything Animal or a subclass of Animal.
You may add Dog, you may add Animal, you may not add Species, you may not add Object. The only things that you can add to a <? super ClassName> are ClassName or subclass of ClassName.
 
aslika bahini
Ranch Hand
Posts: 111
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Bob,

Thanks much Bob for clarifying.

Thanks again
Saritha
 
Bob Ruth
Ranch Hand
Posts: 320
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
My pleasure!
 
Politics n. Poly "many" + ticks "blood sucking insects". Tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic