Multiple inheritance using interfaces in Java - Time & Space Complexity
Start learning this pattern below
Jump into concepts and practice - no test required
We want to understand how the time needed to run code changes when using multiple inheritance with interfaces in Java.
Specifically, how does calling methods from multiple interfaces affect the program's speed as the input grows?
Analyze the time complexity of the following code snippet.
interface A {
void methodA();
}
interface B {
void methodB();
}
class C implements A, B {
public void methodA() { System.out.println("A"); }
public void methodB() { System.out.println("B"); }
}
public class Test {
public static void main(String[] args) {
C obj = new C();
obj.methodA();
obj.methodB();
}
}
This code shows a class implementing two interfaces and calling their methods.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Calling methods from two interfaces once each.
- How many times: Each method is called exactly once; no loops or recursion.
Since there are no loops or repeated calls, the number of operations stays the same no matter how big the input is.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 2 method calls |
| 100 | 2 method calls |
| 1000 | 2 method calls |
Pattern observation: The work does not increase with input size; it stays constant.
Time Complexity: O(1)
This means the time to run the code stays the same no matter how big the input is.
[X] Wrong: "Using multiple interfaces makes the program slower as input grows because it has more methods to call."
[OK] Correct: The number of method calls here is fixed and does not depend on input size, so the time does not grow with input.
Understanding how multiple inheritance with interfaces affects time helps you explain design choices clearly and shows you know how code structure relates to performance.
"What if each method called inside the interfaces contained a loop over an input array? How would the time complexity change?"
Practice
Solution
Step 1: Understand Java inheritance limitations
Java does not allow multiple inheritance with classes to avoid ambiguity known as the diamond problem.Step 2: Role of interfaces in multiple inheritance
Interfaces allow multiple inheritance of type without implementation conflicts, enabling safe multiple inheritance.Final Answer:
To avoid the diamond problem and allow safe multiple inheritance -> Option AQuick Check:
Interfaces solve diamond problem = A [OK]
- Thinking Java supports multiple class inheritance
- Believing interfaces improve speed
- Confusing method overriding prevention
Car implementing two interfaces Engine and Wheels?Solution
Step 1: Recall Java syntax for implementing multiple interfaces
Java uses the keywordimplementsfollowed by a comma-separated list of interfaces.Step 2: Check each option's syntax
public class Car implements Engine, Wheels {} correctly usesimplements Engine, Wheels. Options B and D incorrectly useextendsfor interfaces or mix keywords wrongly. public class Car implements Engine & Wheels {} uses an invalid '&' symbol.Final Answer:
public class Car implements Engine, Wheels {} -> Option CQuick Check:
Multiple interfaces use comma with implements = A [OK]
- Using extends instead of implements for interfaces
- Using '&' instead of commas
- Mixing extends and implements incorrectly
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();
}
}Solution
Step 1: Understand default methods in interfaces
Both interfaces A and B have a default methodshow(). Class C implements both and overridesshow().Step 2: Analyze method calls inside C's show()
C'sshow()callsA.super.show()thenB.super.show(), so it prints "A" then "B" on separate lines.Final Answer:
A B -> Option BQuick Check:
Calling both interface methods prints A then B [OK]
- Expecting compilation error without override
- Confusing order of prints
- Missing super calls causing ambiguity
interface X {
void display();
}
interface Y {
void display();
}
class Z implements X, Y {
// No display() method implemented
}Solution
Step 1: Check interface method requirements
Interfaces X and Y both declaredisplay()method without implementation.Step 2: Check class Z implementation
Class Z implements both interfaces but does not providedisplay()method, so it must be abstract or implement the method.Final Answer:
Class Z must implement display() method or be declared abstract -> Option AQuick Check:
Implement all interface methods or declare abstract = D [OK]
- Assuming no implementation needed if methods have same name
- Thinking interfaces cannot share method names
- Using extends instead of implements for 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.Solution
Step 1: Understand interface implementation and method overriding
FileHandler must implement bothread()andwrite()methods from interfaces.Step 2: Add logging before operations
Override methods in FileHandler to add logging code before performing the actual operation.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 keywordextend. Use abstract class instead of interfaces for multiple inheritance avoids interfaces and is not best for multiple inheritance.Final Answer:
Implement interfaces and add logging inside overridden methods in FileHandler -> Option DQuick Check:
Override interface methods to add behavior = C [OK]
- 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
