0
0
Javaprogramming~20 mins

Multiple inheritance using interfaces in Java - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
πŸŽ–οΈ
Multiple Interface Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2: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();
    }
}
A
Interface A
Interface B
Class C
B
Class C
Interface A
Interface B
C
Interface B
Interface A
Class C
DCompilation error due to multiple inheritance conflict
Attempts:
2 left
πŸ’‘ Hint
Look at how the show() method calls the default methods from both interfaces explicitly.
❓ Predict Output
intermediate
2: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();
    }
}
ARuntime error
BZ display
CCompilation error because X has no default method
DY display
Attempts:
2 left
πŸ’‘ Hint
Class Z provides its own display() method, so it overrides the default.
πŸ”§ Debug
advanced
2: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();
    }
}
ANo error, prints nothing
BRuntime error: method action() not found
CCompilation error: duplicate method action() in interfaces
DCompilation error: class R must implement action()
Attempts:
2 left
πŸ’‘ Hint
Check if class R provides implementation for all abstract methods from interfaces.
πŸ“ Syntax
advanced
2:00remaining
Correct syntax for multiple interface inheritance
Which option correctly declares a class that inherits from two interfaces A and B?
Aclass MyClass inherits A, B {}
Bclass MyClass extends A, B {}
Cclass MyClass implements A, B {}
Dclass MyClass implements (A & B) {}
Attempts:
2 left
πŸ’‘ Hint
In Java, classes use 'implements' keyword for interfaces and separate multiple interfaces with commas.
πŸš€ Application
expert
2: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() {}
}
A3
B2
C4
D5
Attempts:
2 left
πŸ’‘ Hint
Count all methods declared or inherited, considering overrides and duplicates.