0
0
Javaprogramming~5 mins

Super keyword in Java - Time & Space Complexity

Choose your learning style9 modes available
Time Complexity: Super keyword
O(1)
Understanding Time Complexity

Let's explore how using the super keyword affects the time it takes for a program to run.

We want to see how calling a parent class method with super changes the work done as input grows.

Scenario Under Consideration

Analyze the time complexity of the following code snippet.


class Parent {
    void display() {
        System.out.println("Parent display");
    }
}

class Child extends Parent {
    void display() {
        super.display();
        System.out.println("Child display");
    }
}

public class Test {
    public static void main(String[] args) {
        Child c = new Child();
        c.display();
    }
}
    

This code calls a method in the child class that also calls the parent class method using super.

Identify Repeating Operations

Look for repeated actions that take time.

  • Primary operation: Calling display() method once.
  • How many times: The method runs once, calling parent method once inside.
How Execution Grows With Input

Since the method calls happen only once, the work stays the same no matter the input size.

Input Size (n)Approx. Operations
102 method calls
1002 method calls
10002 method calls

Pattern observation: The number of operations does not increase with input size.

Final Time Complexity

Time Complexity: O(1)

This means the time taken stays constant no matter how big the input is.

Common Mistake

[X] Wrong: "Using super makes the program slower as input grows."

[OK] Correct: Calling a parent method with super is just one extra step and does not repeat or grow with input size.

Interview Connect

Understanding how super works helps you explain method calls clearly and shows you know how inheritance affects program flow.

Self-Check

"What if the display() method called super.display() inside a loop that runs n times? How would the time complexity change?"