• 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

OCPJP 7 generics super keyword use

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
//this code does not compile !


But this is allowed for collections




Why?

Thanks
 
Enthuware Software Support
Posts: 4810
52
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

north cartman wrote://this code does not compile !


But this is allowed for collections




Why?

Thanks


Explained in detail here: http://www.angelikalanger.com/GenericsFAQ/FAQSections/TypeParameters.html#FAQ107
 
north cartman
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Not getting it. Looks like I will have to start from scratch.
The article linked has me more confused. Unfortunatley I havent found a book or article that explains things in simple terms like head first. Will keep trying.
Thanks for the link though.

I was hoping there would be a simple answer for why the super bound is allowed in some cases and not in other.
 
north cartman
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Got an insight



Think about why do we need bounds.
We needs bounds so that we can guarantee that the methods we are invoking on <T> would always work
with <T extends Dog> , all Dog methods if used will work .
with <T super Dog> , Dog methods will work only if you use Dog. so whats the point of <T super Dog> ,Got it!

Coming to lists

Why add is allowed only sometimes with super

think about passing a py = list<Puppy> to a method that accepts List<? super Puppy> e.g. insertStuff(py);



Why add is never allowed with extends

think about passing list to a method that accepts List<? extends Pet>

 
Paul Anilprem
Enthuware Software Support
Posts: 4810
52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

north cartman wrote:Not getting it. Looks like I will have to start from scratch.
The article linked has me more confused. Unfortunatley I havent found a book or article that explains things in simple terms like head first. Will keep trying.


There is one trick that has helped me understand complicated stuff over the years. Read good material...slowly...multiple times. You will get it. That FAQ is gold, I can tell you that. Rest is up to you
 
reply
    Bookmark Topic Watch Topic
  • New Topic