Constructor Overloading in Java
Constructor overloading is a technique where a class has more than one constructor, each with a different parameter list. It allows objects to be created in different ways depending on the arguments provided.
Example
class Student {
String name;
int age;
Student() {
this.name = "Unknown";
this.age = 0;
}
Student(String name) {
this.name = name;
this.age = 0;
}
Student(String name, int age) {
this.name = name;
this.age = age;
}
}
Student s1 = new Student();
Student s2 = new Student("Karan");
Student s3 = new Student("Meera", 23);
Rules for Constructor Overloading
- Each constructor must differ in the number, type, or order of parameters
- Constructors share the class name, so overloading relies entirely on parameter lists
- One constructor can call another using
this()to avoid repeating initialization logic
Why Use Constructor Overloading?
- Gives users of the class flexibility to create objects with different amounts of initial information
- Supports default values when certain fields aren't provided
- Improves code usability without duplicating the entire class logic
Master Java with Uncodemy
Hands-on training, live projects, and placement support in our Java Programming Course.
How Constructor Chaining With this() Works
Repeating the same assignments across several constructors invites bugs. Constructor chaining fixes that: one constructor calls another using this(...) so the real initialisation lives in a single place. The this() call must be the very first statement in the constructor.
class Student {
String name;
int age;
Student() {
this("Unknown", 0); // delegates to the two-arg constructor
}
Student(String name) {
this(name, 0); // delegates as well
}
Student(String name, int age) { // the one place that sets fields
this.name = name;
this.age = age;
}
}
Now logic such as validation only has to be written and maintained in the two-argument constructor, and the others simply supply defaults.
Overloading vs a Single Constructor With Defaults
Java has no built-in default parameter values, which is a big reason constructor overloading is so common — each overload stands in for one set of defaults. In some cases a builder pattern or a static factory method reads more clearly than many overloads, especially when several parameters share the same type and the call becomes hard to read.
Pitfalls With Ambiguous or Fragile Calls
- Ambiguity: two overloads that differ only by types the compiler can auto-convert between can make a call ambiguous. Keep parameter lists clearly distinct.
- Same-type confusion:
Student(String, int)andStudent(int, String)are technically distinct but easy for callers to mix up; naming them differently is impossible, so a factory method is often safer. - Forgetting the default constructor: once you declare any constructor, Java stops supplying the free no-argument one. Add it back explicitly if other code relies on it.
- Deep delegation chains that are hard to follow; one or two hops is plenty.
One more rule trips people up: when a constructor uses this() to call another, that call must be the first statement, so you cannot run validation before it. Put shared validation inside the target constructor instead, where every overload funnels through it. For the same reason, a call to a superclass constructor with super() and a call to a sibling with this() cannot both appear in one constructor, since each demands to be first.
Constructor overloading is a form of compile-time polymorphism — the compiler matches the argument list to a constructor before the program runs, just like method overloading.
Takeaway: give each overload a distinct, unambiguous parameter list, funnel the real work through one constructor with this(), and switch to a builder when the number of parameters starts to hurt readability.