Skip to content
Sharp

Core Java · interview question

Given the following code, why does it compile successfully? interface A { default void foo() {} } class B implements A { void foo() { System.out.println('Hello'); } }?

Multiple choiceHardchapter-5-curatedpremiumcore-javadefault-methods-and-multiple-inheritance
java
interface A { default void foo() {} } class B implements A { void foo() { System.out.println('Hello'); } }
Choose one answer
Answer and explanation

Correct answer

D. The class B overrides the default method successfully.

Why

Class B provides its own implementation of foo(), overriding the default method from interface A. This is valid because default methods can be overridden like any other method.

Test yourself on Core Java

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

Start free test
All Core Java questions →