Compile-time polymorphism in Java - Time & Space Complexity
Start learning this pattern below
Jump into concepts and practice - no test required
Let's explore how the time cost grows when using compile-time polymorphism in Java.
We want to see how method overloading affects the number of operations as input changes.
Analyze the time complexity of the following code snippet.
class Calculator {
int add(int a, int b) {
return a + b;
}
int add(int a, int b, int c) {
return a + b + c;
}
}
public class Main {
public static void main(String[] args) {
Calculator calc = new Calculator();
int result = calc.add(5, 10);
int result2 = calc.add(5, 10, 15);
}
}
This code shows method overloading where the same method name handles different numbers of inputs.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Simple addition operations inside overloaded methods.
- How many times: Each method runs once per call; no loops or recursion.
Since each method just adds a fixed number of integers, the work stays almost the same regardless of input size.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 1 or 2 additions per call |
| 100 | Still about 1 or 2 additions per call |
| 1000 | Still about 1 or 2 additions per call |
Pattern observation: The number of operations does not grow with input size; it stays constant.
Time Complexity: O(1)
This means the time to run these methods stays the same no matter how big the input is.
[X] Wrong: "More overloaded methods mean slower performance because the program checks all methods each time."
[OK] Correct: The compiler decides which method to call before running the program, so no extra checks happen at runtime.
Understanding how compile-time polymorphism works helps you explain efficient method calls and shows you know how Java handles method selection quickly.
"What if the overloaded methods used loops inside? How would that change the time complexity?"
Practice
What is compile-time polymorphism in Java?
Solution
Step 1: Understand method overloading
Compile-time polymorphism is also called method overloading, where methods share the same name but differ in parameters.Step 2: Differentiate from runtime polymorphism
Runtime polymorphism uses method overriding, changing behavior based on object type at runtime, not compile-time.Final Answer:
Using the same method name with different parameters in the same class -> Option CQuick Check:
Compile-time polymorphism = method overloading [OK]
- Confusing compile-time with runtime polymorphism
- Thinking method overriding is compile-time polymorphism
- Believing different method names are polymorphism
Which of the following is the correct syntax for method overloading in Java?
public class Calculator {
public int add(int a, int b) { return a + b; }
public int add(int a, int b, int c) { ? }
}Solution
Step 1: Check method parameters
The second add method has three parameters, so it should add all three values.Step 2: Write correct return statement
Return the sum of a, b, and c to correctly overload the add method.Final Answer:
return a + b + c; -> Option AQuick Check:
Overloaded method sums all parameters [OK]
- Returning sum of only two parameters in three-parameter method
- Using wrong operators like multiplication or subtraction
- Syntax errors like missing semicolon
What will be the output of the following Java code?
class Demo {
void show(int a) { System.out.println("Int: " + a); }
void show(String a) { System.out.println("String: " + a); }
}
public class Test {
public static void main(String[] args) {
Demo d = new Demo();
d.show(5);
d.show("Hello");
}
}Solution
Step 1: Identify overloaded methods
There are two show methods: one takes int, the other takes String.Step 2: Match method calls to parameters
d.show(5) calls show(int), printing "Int: 5"; d.show("Hello") calls show(String), printing "String: Hello".Final Answer:
Int: 5\nString: Hello -> Option AQuick Check:
Method overloading calls correct method by parameter type [OK]
- Confusing parameter types and outputs
- Expecting runtime polymorphism behavior
- Thinking it causes compilation error
Find the error in this code snippet related to compile-time polymorphism:
class Test {
void display(int a) { System.out.println(a); }
void display(int a, int b) { System.out.println(a + b); }
void display(int a) { System.out.println(a * 2); }
}Solution
Step 1: Check method signatures
Two methods have the exact same name and parameter list: display(int a).Step 2: Understand method overloading rules
Method overloading requires different parameter lists; duplicate signatures cause compilation error.Final Answer:
Duplicate method display(int a) causes compilation error -> Option BQuick Check:
Duplicate method signatures cause compile error [OK]
- Thinking method bodies affect overloading
- Ignoring duplicate parameter lists
- Assuming code compiles without error
Consider this class:
class Printer {
void print(int a) { System.out.println("Number: " + a); }
void print(String a) { System.out.println("Text: " + a); }
void print(int a, String b) { System.out.println(a + " and " + b); }
}Which call will cause a compile-time error?
Solution
Step 1: Check method signatures
Methods accept (int), (String), and (int, String) parameters.Step 2: Match call parameters
print("Hello", 10) tries to call (String, int), which does not exist, causing compile error.Final Answer:
print("Hello", 10) -> Option DQuick Check:
No matching method for (String, int) call [OK]
- Assuming parameter order doesn't matter
- Thinking all combinations are allowed
- Ignoring method signature mismatch
