public class Animal {}
public class Dog extends Animal {}
public class Cat extends Animal {}
// Usage:
Animal a = new Cat();
Dog d = (Dog) a;What occurs?
- Acheck_circle
ClassCastException at runtime
- B
Compiles and runs successfully
- C
Compile error
- D
Casts but d is null
Explanation
The cast compiles since Cat and Dog both extend Animal, but at runtime the actual object is a Cat, which is not a Dog, so a ClassCastException is thrown.