What is Constructor Overloading?
Constructor overloading explained — multiple constructors, differing parameter lists, compile-time resolution — with Java examples.
Expected Interview Answer
Constructor overloading is defining multiple constructors in the same class that differ in the number, type, or order of their parameters, giving callers several valid ways to create an object.
Like overloaded methods, overloaded constructors share the same name (the class name) but must have distinguishable parameter lists so the compiler can resolve, at compile time, which one to invoke based on the arguments supplied at the call site. This is commonly used to provide sensible defaults — a no-argument constructor that delegates via `this(...)` to a fuller constructor supplying default values for the missing parameters. Constructor overloading is compile-time (static) polymorphism, resolved purely from the argument list, distinct from overriding, which is resolved at runtime based on object type. Overloaded constructors cannot differ only by return type, since constructors have no return type at all.
- Offers multiple convenient ways to instantiate an object
- Supports sensible default values without duplicating field logic
- Improves API usability for consumers of the class
- Resolved safely at compile time, with no runtime ambiguity
AI Mentor Explanation
A cricket-kit vendor lets you order just “bat”, or “bat and pads”, or “full kit with helmet and gloves” — each order form has a different set of fields, but they all ultimately assemble a kit for you. The vendor’s system resolves which packing process to run purely from what you filled in on the form. That is constructor overloading: several constructors with different parameter lists, all creating the same kind of object with varying levels of detail supplied up front.
Step-by-Step Explanation
Step 1
Define the class name once
All overloaded constructors share the class name — that never changes.
Step 2
Vary the parameter list
Differ constructors by parameter count, type, or order — never by return type.
Step 3
Assign or delegate defaults
Simpler constructors often chain via this(...) to a fuller constructor, supplying default values.
Step 4
Let the compiler resolve calls
At each new ClassName(...) call site, the compiler picks the matching constructor based on the arguments.
What Interviewer Expects
- Correct definition: same class name, different parameter lists
- Understanding that resolution happens at compile time
- Awareness that return type cannot distinguish constructors (they have none)
- A practical example showing default-value delegation via this(...)
Common Mistakes
- Trying to overload constructors by return type alone (constructors have no return type)
- Confusing constructor overloading with constructor overriding (constructors are not overridden)
- Writing ambiguous parameter lists the compiler cannot disambiguate
- Duplicating field-assignment code instead of chaining with this(...)
Best Answer (HR Friendly)
“Constructor overloading means writing several constructors for the same class that take different sets of parameters, so callers can create an object in whichever way is most convenient for them. The compiler figures out which constructor to run just from the arguments passed in. It’s common to have a simple constructor delegate to a fuller one using this(...) so default values only live in one place.”
Code Example
class Rectangle {
int width, height;
Rectangle() {
this(1, 1); // default 1x1
}
Rectangle(int side) {
this(side, side); // square
}
Rectangle(int width, int height) {
this.width = width;
this.height = height;
}
}
Rectangle a = new Rectangle(); // 1x1
Rectangle b = new Rectangle(5); // 5x5 square
Rectangle c = new Rectangle(4, 9); // 4x9Follow-up Questions
- Can constructors be overloaded by return type alone?
- How does the compiler resolve which overloaded constructor to call?
- What happens if two overloaded constructors have ambiguous parameter lists?
- How does constructor overloading relate to constructor chaining?
MCQ Practice
1. Constructor overloading requires constructors to differ in?
Overloaded constructors must differ in their parameter list since they share the class name and have no return type.
2. When is the correct overloaded constructor selected?
Constructor overloading is resolved at compile time from the arguments at the call site.
3. Can two constructors be overloaded by return type alone?
Constructors have no return type at all, so return type can never distinguish overloads.
Flash Cards
Constructor overloading in one line? — Multiple constructors in one class differing by parameter list.
Resolved when? — At compile time, based on the arguments supplied at the call site.
Can return type distinguish overloads? — No — constructors have no return type.
Common use? — Simpler constructors chain via this(...) to a fuller one, supplying defaults.