Win a copy of Programmers Guide to Apache Thrift this week in the Open Source forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Knute Snortum
  • Paul Clapham
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Ron McLeod
  • Piet Souris
  • Frits Walraven
Bartenders:
  • Ganesh Patekar
  • Tim Holloway
  • salvin francis

unable to invoke methods when using generic wildcard (super)  RSS feed

 
Ranch Hand
Posts: 47
Debian Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • 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
  • 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
Debian Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • 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
  • 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
Debian Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • 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..
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!