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: 
Your `dogs` variable is of type `List` which is a *raw* type. It is **not** of type `List<Dog>`.

And since it is a raw type, you can add any object to it. Your `Cat` instance is not casted to anything.

Generics only provide compile-time safety and check, they are not used at runtime.

Read more about raw types in the [Java Language Specification 4.8. Raw Types](http://docs.oracle.com/javase/specs/jls/se8/html/jls-4.html#jls-4.8).
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`.