Challenge - 5 Problems
Default Methods Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
β Predict Output
intermediateOutput of default method call in interface
What is the output of this Java code?
Java
interface Speaker { default void sayHello() { System.out.println("Hello from Speaker"); } } class Person implements Speaker { } public class Main { public static void main(String[] args) { Person p = new Person(); p.sayHello(); } }
Attempts:
2 left
π‘ Hint
Remember that default methods in interfaces provide an implementation that classes can inherit.
β Incorrect
The interface Speaker has a default method sayHello(). The class Person implements Speaker but does not override sayHello(), so the default method is called, printing "Hello from Speaker".
β Predict Output
intermediateOutput when overriding default method
What will be printed when running this code?
Java
interface Printer { default void print() { System.out.println("Printing from Printer"); } } class Document implements Printer { public void print() { System.out.println("Printing from Document"); } } public class Main { public static void main(String[] args) { Document doc = new Document(); doc.print(); } }
Attempts:
2 left
π‘ Hint
Check if the class overrides the default method.
β Incorrect
The class Document overrides the default print() method from Printer interface. So when print() is called on Document instance, the overridden method runs, printing "Printing from Document".
β Predict Output
advancedResolving conflict between two default methods
What is the output of this code?
Java
interface A { default void show() { System.out.println("A show"); } } interface B { default void show() { System.out.println("B show"); } } class C implements A, B { public void show() { A.super.show(); } } public class Main { public static void main(String[] args) { C c = new C(); c.show(); } }
Attempts:
2 left
π‘ Hint
When two interfaces have the same default method, the implementing class must resolve the conflict explicitly.
β Incorrect
Class C implements both interfaces A and B which have default show() methods. To resolve the conflict, C overrides show() and calls A's version explicitly using A.super.show(). So output is "A show".
π§ Debug
advancedIdentify the compilation error in default method usage
This code does not compile. What is the cause of the compilation error?
Java
interface X { default void doSomething() { System.out.println("Doing something"); } } class Y implements X { public void doSomething() { System.out.println("Doing something else"); } } public class Main { public static void main(String[] args) { Y y = new Y(); y.doSomething(); } }
Attempts:
2 left
π‘ Hint
Check the access modifier of the overriding method compared to the interface method.
β Incorrect
Interface methods are implicitly public. When overriding, the method in the class must be public or more accessible. Here, doSomething() in Y has default (package-private) access, causing a compilation error.
π§ Conceptual
expertBehavior of default methods with multiple inheritance and diamond problem
Consider interfaces I1 and I2 both have a default method foo(). Class D implements both interfaces but does NOT override foo(). What happens when foo() is called on an instance of D?
Attempts:
2 left
π‘ Hint
Java requires explicit resolution when multiple interfaces provide the same default method.
β Incorrect
When a class implements two interfaces that have the same default method, Java forces the class to override that method to resolve the conflict. Otherwise, it causes a compilation error.
