Bird
Raised Fist0
Javaprogramming~20 mins

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

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
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.

Practice

(1/5)
1. What is the main reason Java uses interfaces for multiple inheritance instead of classes?
easy
A. To avoid the diamond problem and allow safe multiple inheritance
B. Because Java does not support inheritance at all
C. To make code run faster
D. To prevent any method overriding

Solution

  1. Step 1: Understand Java inheritance limitations

    Java does not allow multiple inheritance with classes to avoid ambiguity known as the diamond problem.
  2. Step 2: Role of interfaces in multiple inheritance

    Interfaces allow multiple inheritance of type without implementation conflicts, enabling safe multiple inheritance.
  3. Final Answer:

    To avoid the diamond problem and allow safe multiple inheritance -> Option A
  4. Quick Check:

    Interfaces solve diamond problem = A [OK]
Hint: Interfaces enable multiple inheritance safely in Java [OK]
Common Mistakes:
  • Thinking Java supports multiple class inheritance
  • Believing interfaces improve speed
  • Confusing method overriding prevention
2. Which of the following is the correct syntax for a class Car implementing two interfaces Engine and Wheels?
easy
A. public class Car implements Engine & Wheels {}
B. public class Car extends Engine, Wheels {}
C. public class Car implements Engine, Wheels {}
D. public class Car extends Engine implements Wheels {}

Solution

  1. Step 1: Recall Java syntax for implementing multiple interfaces

    Java uses the keyword implements followed by a comma-separated list of interfaces.
  2. Step 2: Check each option's syntax

    public class Car implements Engine, Wheels {} correctly uses implements Engine, Wheels. Options B and D incorrectly use extends for interfaces or mix keywords wrongly. public class Car implements Engine & Wheels {} uses an invalid '&' symbol.
  3. Final Answer:

    public class Car implements Engine, Wheels {} -> Option C
  4. Quick Check:

    Multiple interfaces use comma with implements = A [OK]
Hint: Use 'implements' with commas for multiple interfaces [OK]
Common Mistakes:
  • Using extends instead of implements for interfaces
  • Using '&' instead of commas
  • Mixing extends and implements incorrectly
3. What will be the output of the following code?
interface A {
    default void show() { System.out.println("A"); }
}
interface B {
    default void show() { System.out.println("B"); }
}
class C implements A, B {
    public void show() {
        A.super.show();
        B.super.show();
    }
}
public class Test {
    public static void main(String[] args) {
        C obj = new C();
        obj.show();
    }
}
medium
A. B\nA
B. A\nB
C. Compilation error due to method conflict
D. No output

Solution

  1. Step 1: Understand default methods in interfaces

    Both interfaces A and B have a default method show(). Class C implements both and overrides show().
  2. Step 2: Analyze method calls inside C's show()

    C's show() calls A.super.show() then B.super.show(), so it prints "A" then "B" on separate lines.
  3. Final Answer:

    A B -> Option B
  4. Quick Check:

    Calling both interface methods prints A then B [OK]
Hint: Use InterfaceName.super.method() to call specific default methods [OK]
Common Mistakes:
  • Expecting compilation error without override
  • Confusing order of prints
  • Missing super calls causing ambiguity
4. Identify the error in the following code snippet:
interface X {
    void display();
}
interface Y {
    void display();
}
class Z implements X, Y {
    // No display() method implemented
}
medium
A. Class Z must implement display() method or be declared abstract
B. No error, code compiles fine
C. Interfaces cannot have methods with same name
D. Class Z should extend interfaces, not implement

Solution

  1. Step 1: Check interface method requirements

    Interfaces X and Y both declare display() method without implementation.
  2. Step 2: Check class Z implementation

    Class Z implements both interfaces but does not provide display() method, so it must be abstract or implement the method.
  3. Final Answer:

    Class Z must implement display() method or be declared abstract -> Option A
  4. Quick Check:

    Implement all interface methods or declare abstract = D [OK]
Hint: Implement all interface methods or declare class abstract [OK]
Common Mistakes:
  • Assuming no implementation needed if methods have same name
  • Thinking interfaces cannot share method names
  • Using extends instead of implements for interfaces
5. Given interfaces Readable and Writable with methods read() and write() respectively, how can you design a class FileHandler that implements both interfaces and provides default logging before each operation? Choose the best approach.
hard
A. Implement both interfaces in FileHandler and override methods to add logging then call interface methods
B. Make FileHandler extend both interfaces and implement methods without logging
C. Use abstract class instead of interfaces for multiple inheritance
D. Implement interfaces and add logging inside overridden methods in FileHandler

Solution

  1. Step 1: Understand interface implementation and method overriding

    FileHandler must implement both read() and write() methods from interfaces.
  2. Step 2: Add logging before operations

    Override methods in FileHandler to add logging code before performing the actual operation.
  3. Step 3: Choose best approach

    Implement interfaces and add logging inside overridden methods in FileHandler correctly describes implementing interfaces and adding logging inside overridden methods. Implement both interfaces in FileHandler and override methods to add logging then call interface methods incorrectly suggests calling interface methods which have no implementation. Make FileHandler extend both interfaces and implement methods without logging uses wrong keyword extend. Use abstract class instead of interfaces for multiple inheritance avoids interfaces and is not best for multiple inheritance.
  4. Final Answer:

    Implement interfaces and add logging inside overridden methods in FileHandler -> Option D
  5. Quick Check:

    Override interface methods to add behavior = C [OK]
Hint: Override interface methods to add extra behavior like logging [OK]
Common Mistakes:
  • Trying to extend interfaces instead of implement
  • Calling interface methods directly which have no body
  • Using abstract class when interfaces are better for multiple inheritance