Method overriding rules 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 changes when using method overriding in Java.
Specifically, how does calling an overridden method affect the program's running time 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 {
@Override
void display() {
System.out.println("Child display");
}
}
public class Test {
public static void main(String[] args) {
Parent obj = new Child();
obj.display();
}
}
This code shows a child class overriding a method from its parent, then calling it through a parent reference.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Single method call to an overridden method.
- How many times: Exactly once in this example.
Since the method call happens once, the time does not grow with input size.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 1 method call |
| 100 | 1 method call |
| 1000 | 1 method call |
Pattern observation: The time stays the same no matter how big the input is.
Time Complexity: O(1)
This means the time to call an overridden method stays constant regardless of input size.
[X] Wrong: "Overriding a method makes the program slower as input grows because it adds extra steps."
[OK] Correct: Calling an overridden method is just one step and does not repeat or grow with input size, so it does not slow down the program as input grows.
Understanding that method overriding does not add hidden time costs helps you explain how Java handles dynamic method calls efficiently.
"What if the overridden method contained a loop that runs n times? How would the time complexity change?"
Practice
Solution
Step 1: Understand method overriding signature rules
Method overriding requires the child method to have the exact same name and parameter list as the parent method.Step 2: Check return type and modifiers
The return type must be the same or a subtype, and the method cannot be static to override.Final Answer:
The method in the child class must have the same name and parameters as in the parent class. -> Option BQuick Check:
Method signature match = D [OK]
- Thinking return type can be different
- Assuming static methods can be overridden
- Changing parameter count in child method
public int calculate(int x)?Solution
Step 1: Match method signature exactly
The overriding method must have the same name and parameter types:calculate(int x).Step 2: Check return type and modifiers
Return type must beintand method must not be static.Final Answer:
public int calculate(int x) { return x * 2; } -> Option AQuick Check:
Exact signature and return type = A [OK]
- Changing return type to void
- Changing parameter type
- Making method static
class Parent {
void show() { System.out.println("Parent"); }
}
class Child extends Parent {
@Override
void show() { System.out.println("Child"); }
}
public class Test {
public static void main(String[] args) {
Parent obj = new Child();
obj.show();
}
}Solution
Step 1: Understand dynamic method dispatch
When a parent reference points to a child object, the overridden child method is called at runtime.Step 2: Check method overriding and call
Theshow()method is overridden in Child, soobj.show()calls Child's version.Final Answer:
Child -> Option DQuick Check:
Overridden method called at runtime = B [OK]
- Expecting parent method output
- Confusing compile-time and runtime method calls
- Ignoring @Override annotation effect
class Parent {
void display() {}
}
class Child extends Parent {
@Override
void display(int x) {}
}Solution
Step 1: Compare method signatures in Parent and Child
Parent hasdisplay()with no parameters; Child hasdisplay(int x)with one parameter.Step 2: Understand @Override annotation rules
@Override requires exact signature match; here, parameters differ, so it's not overriding.Final Answer:
Method display(int x) does not override display() due to different parameters. -> Option CQuick Check:
@Override requires exact signature match = C [OK]
- Ignoring parameter difference
- Thinking @Override can be used on any method
- Assuming method overloading is overriding
class Animal {
Number getValue() { return 10; }
}
class Dog extends Animal {
@Override
Integer getValue() { return 20; }
}Which statement about this overriding is correct?
Solution
Step 1: Check return types in parent and child methods
Parent returnsNumber, child returnsInteger, which is a subclass of Number.Step 2: Understand covariant return types in Java overriding
Java allows child methods to return a subtype of the parent's return type when overriding.Final Answer:
This is valid because Integer is a subclass of Number (covariant return type). -> Option AQuick Check:
Covariant return types allowed = A [OK]
- Thinking return types must be exactly the same
- Assuming @Override forbids different return types
- Confusing overloading with overriding
