Creating References Using Inheritance Hierarchies

AP Computer Science A· difficulty 4/5

<code>Animal a = new Dog(); Dog d = (Dog) a;</code> is

  • A

    Throws compile error

  • B

    Compile-time error

  • C

    Always safe

  • D

    Runtime check; OK if a is actually Dog (else ClassCastException)

    check_circle

Explanation

Downcasts are checked at runtime.

Want 10 more like this — adaptive to your weak spots?

Related questions