Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Generics declaration.

 
Michael Boehm
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would like to initialise a list with a type parameter Sub being a subclass of Par as a specific ArrayList. This is what I "want":



How do I do it properly as the above gets an "Incompatible types" error from the compiler.
 
Pramod P Deore
Ranch Hand
Posts: 632
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It is because it is not allowed .Polymorphic assignments applies only to the base type, not the generic type
parameter. You can say


but you can't say like
 
Marimuthu Madasamy
Ranch Hand
Posts: 72
Java Monad Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can use wildcard with bound:

 
Michael Boehm
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the wild card pointer. Need to review the basics of Generics. This link is useful in case others need to do the same:

Bracha: Generics Tutorial
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic