Question: I have some confusion about generic programming in java:

If Manager is subclass of Employee,

```
Collection<Manager> managers=new Collection<Manager>;  
Collection<Employee> employees=managers;//why illegal?

```

Why the last statement is illegal?

Since according to illustration in the book CORE JAVA,after
the erasion,`Collection<Manager>` and `Collection<Employee>` are
all converted into raw type `Collection`.

Answer: 
*The following code snippet should highlight the problem.*

```
Collection<Manager> managers=new Collection<Manager>;  
Collection<Employee> employees=managers;

employees.add(new Janitor());

// Writing it out with iterator instead of enhanced for-loop
for(Iterator<Manager> it = managers.iterator(); it.hasNext() ; ) {
    Manager manager = it.next(); // ! Here we'll crash with ClassCastException !
    System.out.println(manager.manages());
}  

```

**So, it is not the employees list that is the problem - it is that the managers list can now have other stuff shoved into it.**

You can force it through by casting if you want - but then you probably buy yourself trouble later.

Java's generics guarantee that you will NEVER get such a ClassCastException anywhere IFF you have not made any such errors or warnings.

As a side-note, the exact same situation is actually possible/legal with arrays, and as such can give you ArrayStoreExceptions where you would not expect it.

```
Manager[] managers = new Manager[20];
Employee[] employees = managers; // No error or warning

employees[15] = new Janitor(); // gives ArrayStoreException

```
Forgetting about erasure for the moment: If you were allowed to assign a value of type Collection[Manager] to a variable of type Collection[Employee], then when you tried to, say add an Employee to the collection (which the variable name indicates is definitely legal), the addition would fail because what the variable is pointing to is actually a collection of Managers.

Java tries to keep you honest and consistent this way.