• 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

unable to invoke methods when using generic wildcard (super)

 
Ranch Hand
Posts: 47
Eclipse IDE Debian Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Holla guys,

I am currently working on generics chapter from k&b. i was just wondering if there is any way to invoke instance methods while using super wildcard. i can call them perfectly while using extends keyword. below is the code.



can anyone help me out with this please.
thanks in advance
 
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When you say ? super Cat, in could contain anything above Cat in the hierarchy. In other words, it could be an Object. So there's no way you can safely cast things to a Cat and call methods (unless you do it yourself, explicitly).

? super Cat comes in useful when you want to add things to the collection. It's saying "you can safely put any Cat you want in here". Whereas ? extends Cat means "everything in here is definitely a Cat". These aren't the same thing.
 
Chaitanya Kidambi
Ranch Hand
Posts: 47
Eclipse IDE Debian Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Matthew Brown wrote:When you say ? super Cat, in could contain anything above Cat in the hierarchy. In other words, it could be an Object. So there's no way you can safely cast things to a Cat and call methods (unless you do it yourself, explicitly).

? super Cat comes in useful when you want to add things to the collection. It's saying "you can safely put any Cat you want in here". Whereas ? extends Cat means "everything in here is definitely a Cat". These aren't the same thing.




Hello Matthew Brown,

Thanks for the quick reply.

i was just wondering if theres any way. and how can we do the casting explicitly in this senario. any idea /.?

thanks


 
Matthew Brown
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, casting explicitly would be something like this:
You need the check because the list might contain things that aren't Cats.

(Cat extends Dog. Really? I know a few cats that might disagree. )
 
Chaitanya Kidambi
Ranch Hand
Posts: 47
Eclipse IDE Debian Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Matthew Brown wrote: Really? I know a few cats that might disagree. )




hahahah, i am bored of cat extending animal examples ....
anyways, i get it now. thanks a lot mate..
 
reply
    Bookmark Topic Watch Topic
  • New Topic