QuestionQ29

Working with Inheritance

Given the following code:

Question Image

What is the outcome?

  • A Base DerivedA
  • B Base DerivedB
  • C DerivedB DerivedB
  • D DerivedB DerivedA
  • E A ClassCastException is thrown at runtime.
Explanation

The object created is a DerivedB. Both casts succeed because DerivedB inherits from DerivedA and Base. Overridden instance methods use runtime dispatch, so both calls to test() invoke DerivedB.test(), producing DerivedB DerivedB.

Community Discussion

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