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

```
if Y <-- X i.e. X i a type of Y, then you can substitue Y in place of X. However, that's not to say that a list of X is the same as a list of Y. This makes 

```
Collection<Employee> employees=managers; 

```

illegal