Constructor overloading in Java - Time & Space Complexity
Start learning this pattern below
Jump into concepts and practice - no test required
Let's see how the time it takes to create objects changes when we have multiple constructors.
We want to know how the number of constructors affects the work done when making an object.
Analyze the time complexity of the following code snippet.
public class Box {
int width, height, depth;
public Box() {
width = height = depth = 0;
}
public Box(int w, int h, int d) {
width = w;
height = h;
depth = d;
}
}
This code shows two constructors for the Box class: one with no inputs and one with three inputs.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Assigning values to the object's fields inside the constructor.
- How many times: Each constructor runs once when an object is created; no loops or repeated steps inside.
Creating an object runs a fixed number of steps depending on the constructor used.
| Input Size (n) | Approx. Operations |
|---|---|
| 1 | 3 assignments |
| 10 | Each object creation does 3 assignments, repeated 10 times |
| 100 | Each object creation does 3 assignments, repeated 100 times |
Pattern observation: The work per object is constant; total work grows linearly with how many objects you create.
Time Complexity: O(1)
This means creating one object with any constructor takes a fixed amount of time, no matter what.
[X] Wrong: "More constructors mean creating an object takes longer time."
[OK] Correct: Only the chosen constructor runs when creating an object, so the number of constructors does not affect the time for one object.
Understanding constructor overloading helps you explain how object creation works efficiently, a useful skill in many coding tasks.
"What if a constructor called another constructor inside it? How would the time complexity change?"
Practice
What is constructor overloading in Java?
Solution
Step 1: Understand constructor overloading definition
Constructor overloading means having more than one constructor in a class, each with a different set of parameters.Step 2: Check options against definition
Having multiple constructors with different parameter lists in the same class. correctly states this. Options B, C, and D are incorrect because they describe invalid or unrelated concepts.Final Answer:
Having multiple constructors with different parameter lists in the same class. -> Option AQuick Check:
Constructor overloading = multiple constructors with different parameters [OK]
- Thinking constructors must have different names
- Confusing overloading with overriding
- Believing constructors cannot have parameters
Which of the following constructor declarations is correct for overloading?
public class Car {
public Car() { }
public Car(String model) { }
public Car(int year) { }
public Car(String model, int year) { }
}Solution
Step 1: Check parameter lists of constructors
Each constructor has a unique parameter list: no parameters, one String, one int, and two parameters.Step 2: Verify constructor overloading rules
Constructors can share the same name but must differ in parameters. This code follows that rule.Final Answer:
All constructors have different parameter lists, so all are correct. -> Option CQuick Check:
Different parameters = valid overloading [OK]
- Thinking constructors need different names
- Believing constructors cannot have parameters
- Confusing method overloading rules with constructors
What will be the output of the following code?
class Box {
int width, height;
Box() {
width = 10;
height = 10;
}
Box(int w, int h) {
width = w;
height = h;
}
void display() {
System.out.println(width + "," + height);
}
}
public class Test {
public static void main(String[] args) {
Box b1 = new Box();
Box b2 = new Box(5, 15);
b1.display();
b2.display();
}
}Solution
Step 1: Identify which constructors are called
b1 uses the no-argument constructor setting width and height to 10. b2 uses the two-parameter constructor setting width=5 and height=15.Step 2: Understand display output
b1.display() prints "10,10" and b2.display() prints "5,15" on separate lines.Final Answer:
10,10 5,15 -> Option AQuick Check:
Constructor sets values correctly, output matches [OK]
- Assuming default constructor sets zero values
- Mixing order of printed outputs
- Thinking overloading causes errors here
Find the error in the following code snippet:
class Person {
String name;
int age;
Person(String n) {
name = n;
}
Person(String n, int a) {
name = n;
age = a;
}
Person() {
this("Unknown");
this(0);
}
}Solution
Step 1: Analyze constructor chaining rules
In Java, a constructor can call only one other constructor using 'this()' and it must be the first statement.Step 2: Identify the error in the no-argument constructor
The no-argument constructor calls 'this("Unknown")' and then 'this(0)', which is not allowed.Final Answer:
Cannot call two constructors using 'this()' in the same constructor. -> Option BQuick Check:
Only one 'this()' call allowed per constructor [OK]
- Trying to call multiple constructors with 'this()'
- Thinking constructor names must differ
- Forgetting 'this()' must be first statement
Consider a class Employee with overloaded constructors:
class Employee {
String name;
int id;
double salary;
Employee(String name) {
this.name = name;
this.id = 0;
this.salary = 0.0;
}
Employee(String name, int id) {
this(name);
this.id = id;
}
Employee(String name, int id, double salary) {
this(name, id);
this.salary = salary;
}
void display() {
System.out.println(name + "," + id + "," + salary);
}
}
public class Main {
public static void main(String[] args) {
Employee e = new Employee("Alice", 101, 75000.0);
e.display();
}
}What will be the output when running Main?
Solution
Step 1: Trace constructor calls for Employee("Alice", 101, 75000.0)
The three-parameter constructor calls the two-parameter constructor with ("Alice", 101), which calls the one-parameter constructor with ("Alice").Step 2: Understand field assignments
The one-parameter constructor sets name="Alice", id=0, salary=0.0. The two-parameter constructor updates id=101. The three-parameter constructor updates salary=75000.0.Step 3: Output values
After all calls, name="Alice", id=101, salary=75000.0, so display prints "Alice,101,75000.0".Final Answer:
Alice,101,75000.0 -> Option DQuick Check:
Constructor chaining updates fields stepwise [OK]
- Assuming fields reset after each constructor call
- Thinking constructor chaining causes errors
- Ignoring order of field assignments
