Implementing interfaces in Java - Time & Space Complexity
Start learning this pattern below
Jump into concepts and practice - no test required
When we implement interfaces in Java, we write methods that follow a contract. It's important to see how the time it takes to run these methods changes as the input grows.
We want to know: how does the running time grow when the input size increases in these implementations?
Analyze the time complexity of the following code snippet.
public interface Printer {
void printAll(String[] messages);
}
public class SimplePrinter implements Printer {
public void printAll(String[] messages) {
for (String msg : messages) {
System.out.println(msg);
}
}
}
This code defines an interface with a method to print all messages, and a class that implements it by printing each message one by one.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Looping through the array of messages to print each one.
- How many times: Once for each message in the input array.
As the number of messages grows, the time to print them grows too, because each message is handled one by one.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 print operations |
| 100 | 100 print operations |
| 1000 | 1000 print operations |
Pattern observation: The time grows directly with the number of messages; doubling messages doubles the work.
Time Complexity: O(n)
This means the time to run the printAll method grows in a straight line with the number of messages.
[X] Wrong: "Implementing an interface method always takes constant time because it's just one method call."
[OK] Correct: The method can do work that depends on input size, like looping through an array, so time can grow with input.
Understanding how your interface methods scale with input size shows you can write efficient code and reason about performance, a key skill in real projects.
"What if the printAll method called another method inside the loop that itself loops over the messages? How would the time complexity change?"
Practice
Solution
Step 1: Understand Java class and interface relationship
In Java, a class follows an interface by using a specific keyword to promise it will provide all methods declared in the interface.Step 2: Identify the correct keyword
The keyword to make a class follow an interface isimplements, notextendswhich is for classes inheriting other classes.Final Answer:
implements -> Option DQuick Check:
Class follows interface = implements [OK]
- Using extends instead of implements for interfaces
- Confusing inherits keyword which doesn't exist in Java
- Using uses keyword which is invalid
Car that implements interface Vehicle?Solution
Step 1: Identify class and interface keywords
A class is declared withclass, and interfaces withinterface. Here, Car is a class, Vehicle is an interface.Step 2: Use correct syntax for implementing interface
The class Car must useimplementskeyword to follow Vehicle interface. Soclass Car implements Vehicle {}is correct.Final Answer:
class Car implements Vehicle {} -> Option BQuick Check:
Class + implements + Interface = correct syntax [OK]
- Using extends instead of implements for interfaces
- Declaring class as interface
- Using invalid keyword uses
interface Printer {
void print();
}
class Document implements Printer {
public void print() {
System.out.println("Printing document");
}
}
public class Main {
public static void main(String[] args) {
Printer p = new Document();
p.print();
}
}Solution
Step 1: Check if interface method is implemented
The interface Printer declares method print(). The class Document implements Printer and provides public void print() method, so no error.Step 2: Trace main method execution
Main creates Printer reference p to new Document object and calls p.print(). This calls Document's print() which prints "Printing document".Final Answer:
Printing document -> Option AQuick Check:
Implemented method runs and prints output [OK]
- Forgetting to make print() public causes compile error
- Assuming interface methods run automatically without implementation
- Confusing runtime error with compile error
interface Animal {
void sound();
}
class Dog implements Animal {
void sound() {
System.out.println("Bark");
}
}Solution
Step 1: Check method visibility in interface implementation
Interface methods are implicitly public. When implementing, the method must be declared public in the class.Step 2: Identify method declaration in Dog class
Dog's sound() method has default (package-private) visibility, missing public keyword, causing compile error.Final Answer:
Method sound() must be public in Dog class -> Option CQuick Check:
Interface methods require public implementation [OK]
- Omitting public keyword on implemented methods
- Using extends instead of implements for interfaces
- Thinking interface methods can be private
Calculator with methods add(int a, int b) and subtract(int a, int b), which class correctly implements it to return the sum and difference respectively?Solution
Step 1: Check method signatures and visibility
Interface methods are public and return int. So implementing methods must be public and return int with same parameters.Step 2: Analyze each option
class Calc implements Calculator { public int add(int a, int b) { return a + b; } public int subtract(int a, int b) { return a - b; } } matches signatures exactly with public int return type. class Calc implements Calculator { int add(int a, int b) { return a + b; } int subtract(int a, int b) { return a - b; } } misses public keyword. class Calc extends Calculator { public int add(int a, int b) { return a + b; } public int subtract(int a, int b) { return a - b; } } uses extends which is invalid for interfaces. class Calc implements Calculator { public void add(int a, int b) { System.out.println(a + b); } public void subtract(int a, int b) { System.out.println(a - b); } } changes return type to void, which is incorrect.Final Answer:
class Calc implements Calculator { public int add(int a, int b) { return a + b; } public int subtract(int a, int b) { return a - b; } } -> Option AQuick Check:
Match method signatures exactly with public and return type [OK]
- Omitting public keyword on methods
- Using extends instead of implements for interfaces
- Changing return types or parameters
