Challenge - 5 Problems
Multiple Interface Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
β Predict Output
intermediate2:00remaining
Output of multiple interface inheritance
What is the output of this Java program that uses multiple interfaces?
Java
interface A { default void show() { System.out.println("Interface A"); } } interface B { default void show() { System.out.println("Interface B"); } } class C implements A, B { public void show() { A.super.show(); B.super.show(); System.out.println("Class C"); } } public class Test { public static void main(String[] args) { C obj = new C(); obj.show(); } }
Attempts:
2 left
π‘ Hint
Look at how the show() method calls the default methods from both interfaces explicitly.
β Incorrect
The class C overrides show() and calls both interface default methods using InterfaceName.super.show(), then prints its own message. So the output is Interface A, Interface B, then Class C.
β Predict Output
intermediate2:00remaining
Which method is called in multiple interface inheritance?
Given these interfaces and class, what will be printed when calling obj.display()?
Java
interface X { void display(); } interface Y { default void display() { System.out.println("Y display"); } } class Z implements X, Y { public void display() { System.out.println("Z display"); } } public class Main { public static void main(String[] args) { Z obj = new Z(); obj.display(); } }
Attempts:
2 left
π‘ Hint
Class Z provides its own display() method, so it overrides the default.
β Incorrect
Even though interface Y has a default display(), class Z overrides it with its own display() method, so "Z display" is printed.
π§ Debug
advanced2:00remaining
Identify the error in multiple interface inheritance
What error will this code produce when compiled?
Java
interface P { void action(); } interface Q { void action(); } class R implements P, Q { // No action() method implemented } public class Demo { public static void main(String[] args) { R obj = new R(); obj.action(); } }
Attempts:
2 left
π‘ Hint
Check if class R provides implementation for all abstract methods from interfaces.
β Incorrect
Both interfaces P and Q declare abstract method action(). Class R implements both but does not provide action(), so compilation fails.
π Syntax
advanced2:00remaining
Correct syntax for multiple interface inheritance
Which option correctly declares a class that inherits from two interfaces A and B?
Attempts:
2 left
π‘ Hint
In Java, classes use 'implements' keyword for interfaces and separate multiple interfaces with commas.
β Incorrect
Java uses 'implements' keyword followed by comma-separated interface names to inherit multiple interfaces. 'extends' is for classes or interfaces extending interfaces.
π Application
expert2:00remaining
Determine the number of methods in the resulting class
Given these interfaces and class, how many distinct methods does class D have after compilation?
Java
interface I1 { default void m1() {} void m2(); } interface I2 { default void m1() {} void m3(); } class D implements I1, I2 { public void m1() {} public void m2() {} public void m3() {} }
Attempts:
2 left
π‘ Hint
Count all methods declared or inherited, considering overrides and duplicates.
β Incorrect
Class D overrides the conflicting default m1() from both I1 and I2, and implements the abstract m2() and m3(). Thus, it has three distinct methods: m1(), m2(), and m3().