• 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 and Polymorphism

 
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am facing a problem to implement the Polymorphism within a Generic List.

I am having a class like

And another class

One more class

Now in my Mainclass


the error is....
The method doStuff(List<CoreClass> in the type UtilityClass is not applicable for the arguments (List<CoreClass>

So anybody please flash on the reason why the class could not be compiled and what is the wrong with implementing generics with polymorphism or will you please guide me that how can I achieve polymorphism within generic lists ???

Thanks in advance,

With Best Regards,
Ishaan
 
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
you can't use ploymorphism as such in generics.
try doing this

but then you won't be able to structurally modify the list inside function.
there's a valid and logical reason for that.
i suggest going through Sun's tutorial on generics.
Generics
 
Ranch Hand
Posts: 1970
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A List<CoreClass> means a List whose component type is CoreClass, not a List whose component type is anything else.

If you wanted to say your method would accept a List whose component type is CoreClass or any subclass, you want a List<? extends CoreClass>.

Note that you can store subclasses of CoreClass, such as MyClass, in a List<CoreClass>. That's a slightly different issue.

This can seem weird at first, but it does all make sense. I suggest you read about "wildcards" in the Sun Java Generics tutorial (Google it).
 
Peter Chase
Ranch Hand
Posts: 1970
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by gaurav abbi:
you can't use ploymorphism as such in generics.



Using generics certainly doesn't mean you can't use polymorphism. But you do need to understand exactly how it all works, so you know what you can and cannot expect to happen.

As already suggested, please read the tutorial, particularly the bit on wildcards, carefully, and all will become clear.
 
reply
    Bookmark Topic Watch Topic
  • New Topic