This post only holds a few important points one needs to remember  about polymorphism and generics,

1.Polymorphism applies only and only to base types and never for type variables,

E.g

List<String> ls=new ArrayList<String>();

The above statement compiles fine and I was able to use polymorphism for the base types i.e. ArrayList was assigned to List as List was the super type of ArrayList, but the statement below never seems to works,

List<Object> ls=new ArrayList<String>();

Polymorphism is never allowed for type variables. If the type variable is “X” then the reference will only point to the variables of type “X”, and never a super or subtype.

Advertisements