Question: Given the following class structure:

```
class Animal {}
class Dog extends Animal {}
class Cat extends Animal {}

```

And creating a list using generics only on the initialization and not on the reference:

```
List dogs = new ArrayList<Dog>();

```

Why doesn't this code generate a ClassCastException?

```
dogs.add(new Cat()); // How is Cat casted to Dog??!
Cat cat = (Cat) dogs.get(0); // Return perfectly valid Cat object.

```

I understand that there will be no compilation error since the `list` reference does not have any generics and will accept any `Object`. But why doesn't the JVM generate any exception when I try to add a Cat object?

Answer: 
For the compiler, `List dogs` is equivalent to `List<Object> dogs` so the compiler accepts adding instances of any type.

Note that the type of the list considered by the compiler is the raw type `List` (the type of the *local variable* on the left-hand side of the assignment), even if the right-hand side creates an instance of the generic type with the `Dog` parameter.
Generics are not retained at run time. So an `ArrayList<Dog>` is just an`ArrayList`. That's why no exception can be thrown at run time. The compiler could have caught this error at compile time if you haven't used a raw type `List`.