Two Laptop Bag
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

Win a copy of OCA Java SE 8 Programmer I Study Guide this week in the OCAJP 8 forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Generics declaration." Watch "Generics declaration." New topic

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: 632

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
I agree. Here's the link:
subject: Generics declaration.
It's not a secret anymore!