Skip to content
Sharp

Object-oriented programming · interview question

What prints here? Product item = new DigitalProduct(); System.out.println(item.label());?

Multiple choiceMediumchapter-5-curatedpremiumoopmethod-overriding
Choose one answer
Answer and explanation

Correct answer

B. The DigitalProduct label, because dynamic dispatch uses the runtime object type

Why

For overridden instance methods, Java uses the runtime object type, not the reference type, to pick the implementation. That is the core of dynamic dispatch and runtime polymorphism.

Test yourself on Object-oriented programming

A free test with instant, explained results. No account needed.

Start free test
All Object-oriented programming questions →