Question: what is the difference between upcasting and downcasting with respect to class variable?
Answer: Upcasting is casting to a supertype, while downcasting is casting to a subtype. Upcasting is always allowed, but downcasting involves a type check and can throw a ClassCastException . ... In this case, the cast is possible because at runtime animal is actually a Dog even though the static type of animal is Animal .