aspose file tools*
The moose likes Beginning Java and the fly likes Generics declaration. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Generics declaration." Watch "Generics declaration." New topic
Author

Generics declaration.

Michael Boehm
Ranch Hand

Joined: Jun 02, 2010
Posts: 51
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

Joined: Jul 15, 2008
Posts: 629

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


Life is easy because we write the source code.....
Marimuthu Madasamy
Ranch Hand

Joined: Jun 07, 2007
Posts: 72

You can use wildcard with bound:



- Marimuthu Madasamy
Michael Boehm
Ranch Hand

Joined: Jun 02, 2010
Posts: 51
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
 
Don't get me started about those stupid light bulbs.
 
subject: Generics declaration.