Default methods 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 it takes to run code with default methods changes as the input grows.
How does adding a default method affect the speed of running interface methods?
Analyze the time complexity of the following code snippet.
interface Calculator {
default int add(int a, int b) {
return a + b;
}
}
class SimpleCalculator implements Calculator {
public int add(int a, int b) {
return Calculator.super.add(a, b);
}
}
public class Main {
public static void main(String[] args) {
SimpleCalculator calc = new SimpleCalculator();
for (int i = 0; i < 1000; i++) {
System.out.println(calc.add(i, i));
}
}
}
This code uses a default method in an interface and calls it many times in a loop.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Calling the default method
addinside the loop. - How many times: The loop runs 1000 times, so the method is called 1000 times.
Each time the loop runs, it calls the default method once. So the total work grows as the number of loop runs grows.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 calls to the default method |
| 100 | 100 calls to the default method |
| 1000 | 1000 calls to the default method |
Pattern observation: The number of operations grows directly with the input size.
Time Complexity: O(n)
This means the time to run the code grows in a straight line as the input size grows.
[X] Wrong: "Using default methods makes the code run slower because they add extra overhead."
[OK] Correct: Default methods are just like normal methods and calling them inside a loop grows time linearly, not slower or faster than usual.
Understanding how default methods affect time helps you explain interface design choices clearly and shows you know how code runs as it grows.
"What if the default method called another method inside a loop? How would the time complexity change?"
Practice
default methods in Java interfaces?Solution
Step 1: Understand interface method rules before Java 8
Interfaces could only declare abstract methods without bodies, forcing all implementing classes to define them.Step 2: Role of default methods
Default methods allow interfaces to provide method bodies, so new methods can be added without breaking existing classes.Final Answer:
To allow interfaces to have method bodies without breaking existing implementations -> Option DQuick Check:
Default methods add bodies to interfaces safely [OK]
- Confusing default methods with abstract methods
- Thinking default methods enable multiple inheritance of classes
- Believing default methods are private helper methods
Solution
Step 1: Recall default method syntax
Default methods start with the keyworddefault, followed by the return type, method name, and body.Step 2: Check each option
default void show() { System.out.println("Hello"); } matches the correct syntax:default void show() { ... }. Others have incorrect order or keywords.Final Answer:
default void show() { System.out.println("Hello"); } -> Option AQuick Check:
default + return type + method name + body = correct syntax [OK]
- Placing default keyword after return type
- Using default as a method modifier incorrectly
- Omitting method body for default methods
interface A {
default void greet() {
System.out.println("Hello from A");
}
}
class B implements A {
public void greet() {
System.out.println("Hello from B");
}
}
public class Test {
public static void main(String[] args) {
A obj = new B();
obj.greet();
}
}Solution
Step 1: Understand method overriding with default methods
Class B overrides the defaultgreet()method from interface A with its own implementation.Step 2: Determine which greet() is called
At runtime, the overridden method in class B is called, printing "Hello from B".Final Answer:
Hello from B -> Option AQuick Check:
Overridden method in class wins [OK]
- Assuming default method runs instead of overridden
- Expecting compilation error due to default method
- Confusing runtime and compile-time behavior
interface X {
default void display() {
System.out.println("X display");
}
}
interface Y {
default void display() {
System.out.println("Y display");
}
}
class Z implements X, Y {
public void display() {
// ???
}
}What should be done inside class Z's
display() method to fix the error?Solution
Step 1: Understand diamond problem with default methods
Class Z implements two interfaces X and Y, both having default display() methods, causing ambiguity.Step 2: Resolve ambiguity by overriding and calling specific interface method
Class Z must override display() and explicitly call one interface's default method usingX.super.display()orY.super.display().Final Answer:
Call X.super.display() or Y.super.display() to resolve ambiguity -> Option BQuick Check:
Explicit super call fixes default method conflict [OK]
- Ignoring ambiguity and expecting code to compile
- Trying to remove interfaces instead of overriding
- Making method abstract in a concrete class
interface Printer {
default void print() {
System.out.println("Printing document");
}
}
interface Scanner {
default void print() {
System.out.println("Scanning document");
}
}
class MultiFunctionDevice implements Printer, Scanner {
public void print() {
// Combine both behaviors here
}
}Which code inside
print() correctly combines both default methods?Solution
Step 1: Understand calling multiple default methods
To combine behaviors, the class must explicitly call each interface's default method usingInterfaceName.super.method().Step 2: Check each option
Printer.super.print(); Scanner.super.print(); correctly calls both default methods. print(); print(); causes infinite recursion. super.print(); is invalid syntax. Printer.print(); Scanner.print(); is invalid because interfaces cannot be called like classes.Final Answer:
Printer.super.print(); Scanner.super.print(); -> Option CQuick Check:
Use InterfaceName.super.method() to call multiple defaults [OK]
- Calling method recursively causing stack overflow
- Using super.print() without interface name
- Trying to call interface methods like static methods
