• 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

generics question..

 
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can any body explain me how polymorphism works with generics pbly with a good example and the minor glitches to remember for the SCJP6 exam ?
Also pl correct me if I am wrong...
You can in no way add to the collection using the wild card notation the subtypes of the types you specified as part of your signature
but
you can just allow to pass them(subtypes) as a actual parameter into the method signature....say for example
is a method.
you can call this method saying addme ( Doglist ) where doglist is a list of type Dog ...but you cannot actually add the elements of type dog to this collection where it actually expects the elements to be of type animal and only animal.
And if I am right in this, my question is in what cases do we use this kind of scenarios wherein we can just pass in as part of the method arguments but we cannot actually ADD anything of the subtype or the super type into the collection. I mean how is this concept being used in the real world java programes...I am new to java programming so pl bare with me if I ask petty issues.
 
Sheriff
Posts: 9707
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you try, you cannot add elements to this collection, but you can surely retrieve elements from this collection. So you might need a method which takes a list of animals and do something on the objects which all animals can do

 
medhaj hambi
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
but here's my questions though ....okay...you can call the methods which are being used by the animals class...thats true...I agree with you...
but....you pass in say ....doglist i.e list doglist<dog> into the method as argument...and then, can you actually use those dog objects to call the methods of the animal class (animal being its super class) like say for example...doeat() or some thing like that ???

If that is possible, the only thing that is not possible doing w.r.t typed generics is that you cannot basically ADD the subtypes of the types you specified...Am I right in assuming this ? or

Are there any other minor gliteches you can think of that I need to understand here ..?
 
Ankit Garg
Sheriff
Posts: 9707
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Of course you can call doEat on an instance of Dog class. Super class methods can be called on a sub-class object (using the super-class or sub-class reference)...
 
CAUTION! Do not touch the blades on your neck propeller while they are active. Tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic