Skip to content
Sharp

Core Java · interview question

Consider a class hierarchy where a class 'Product' extends 'Item' and implements 'Sellable'. If 'Item' has a method 'display()' and 'Sellable' has a default method 'display()', which method is called on a 'Product' instance?

Multiple choiceHardchapter-5-curatedpremiumcore-javadefault-methods-and-multiple-inheritance
Choose one answer
Answer and explanation

Correct answer

D. The method in 'Item'

Why

In Java, class methods take precedence over interface default methods. Therefore, if the class 'Product' inherits 'display()' from 'Item', it will use that method instead of the default method in 'Sellable'.

Test yourself on Core Java

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

Start free test
All Core Java questions →