QuestionQ134

Working with Inheritance

Consider the following:

Question Image

What is the outcome?

  • A A B
  • B A C
  • C C C
  • D A ClassCastException is thrown only at line n1.
  • E A ClassCastException is thrown only at line n2.
Explanation

The reference b2 has runtime type C, which cannot be cast to sibling subclass B. The downcast at line n1 throws a ClassCastException, and normal execution stops before line n2 or either test() call can run.

Community Discussion

No comments yet. Be the first to start the discussion!