Super keyword in Java - Time & Space Complexity
Start learning this pattern below
Jump into concepts and practice - no test required
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.
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.
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.
Since the method calls happen only once, the work stays the same no matter the input size.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 2 method calls |
| 100 | 2 method calls |
| 1000 | 2 method calls |
Pattern observation: The number of operations does not increase with input size.
Time Complexity: O(1)
This means the time taken stays constant no matter how big the input is.
[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.
Understanding how super works helps you explain method calls clearly and shows you know how inheritance affects program flow.
"What if the display() method called super.display() inside a loop that runs n times? How would the time complexity change?"
Practice
super keyword do in Java?Solution
Step 1: Understand the role of
Thesupersuperkeyword is used to refer to the parent class's members (methods or variables) from a child class.Step 2: Compare options with definition
Only It accesses methods and variables from the parent class. correctly describes this behavior. Other options describe unrelated actions.Final Answer:
It accesses methods and variables from the parent class. -> Option CQuick Check:
super accesses parent members = A [OK]
- Thinking super creates new objects
- Confusing super with this keyword
- Assuming super ends program
Solution
Step 1: Recall syntax for parent constructor call
In Java,super()is used inside a child constructor to call the parent class constructor.Step 2: Evaluate options
Only super(); uses the correct keywordsuper(). Others are invalid or refer to different concepts.Final Answer:
super(); -> Option AQuick Check:
Parent constructor call = super() [OK]
- Using this() instead of super()
- Trying to call parent() which is invalid
- Confusing base() with super()
class Parent {
int x = 10;
}
class Child extends Parent {
int x = 20;
void printX() {
System.out.println(super.x);
}
}
public class Test {
public static void main(String[] args) {
Child c = new Child();
c.printX();
}
}Solution
Step 1: Understand variable hiding and super usage
The child class has its ownx = 20, butsuper.xaccesses the parent'sxwhich is 10.Step 2: Trace the print statement
The methodprintX()printssuper.x, so it prints 10.Final Answer:
10 -> Option DQuick Check:
super.x accesses parent variable = 10 [OK]
- Printing child variable instead of parent
- Confusing super.x with this.x
- Expecting compilation error
class Parent {
void show() {
System.out.println("Parent show");
}
}
class Child extends Parent {
void show() {
super.show();
System.out.println("Child show");
}
void display() {
super();
}
}Solution
Step 1: Check usage of super in methods
Callingsuper.show()inside overridden method is valid to call parent method.Step 2: Analyze
super()call in display()super()can only be used to call parent constructor inside child constructor, not as a method call elsewhere.Final Answer:
super() cannot be called like a method in display() -> Option BQuick Check:
super() only in constructor = D [OK]
- Using super() outside constructor
- Thinking super.show() is invalid
- Believing overriding is not allowed
class Animal {
String name;
Animal(String name) {
this.name = name;
}
void sound() {
System.out.println("Animal sound");
}
}
class Dog extends Animal {
Dog() {
super("Dog");
}
void sound() {
super.sound();
System.out.println("Bark");
}
}
public class Test {
public static void main(String[] args) {
Dog d = new Dog();
d.sound();
System.out.println(d.name);
}
}What is the output when running
Test.main()?Solution
Step 1: Understand constructor chaining
TheDogconstructor callssuper("Dog"), settingnameto "Dog" inAnimal.Step 2: Trace the
sound()method callDog.sound()callssuper.sound()which prints "Animal sound", then prints "Bark".Step 3: Print the
Printingnamefieldd.nameoutputs "Dog".Final Answer:
Animal sound Bark Dog -> Option AQuick Check:
super() sets name, super.sound() prints parent sound = A [OK]
- Expecting Dog before Animal sound
- Confusing order of prints
- Thinking super() causes error
