First of all an extended comprehension before we are going into the specials of generics; let’s start with the following declarations:
Employee () { … }
SalesPerson extends Employee () { … }
ControllingStaff extends Employee () { … }
A cast in an upper class is possible without problems:
Employee a = new SalesPerson ();
Employeer b = new ControllingStaff ();
Same is applicable for arrays:
Employee [ ] c = {new SalesPerson(), new ControllingStaff () };
But this is not working for Collections:
List <Employee> b = new ArrayList <SalesPerson>();
What is working is the assignment of sub-classes to the collection of the upper class :
List <Employee> d = new ArrayList <Employee>();
d.add(new SalesPerson());
d.add(new ControllingStaff());
No warning is produced, the code is working fine through the hole program-flow.