Java supports constructor overloading, which allows a class to have more than one constructor with different parameter lists. The appropriate constructor is selected at compile time based on the arguments passed during object creation. Constructor overloading enables objects to be initialized in multiple ways, improving flexibility and code clarity.
When do we need Constructor Overloading?
Constructor overloading is useful when:
- Objects need to be initialized with different sets of data
- Default values are required in some cases
- Simplified object creation is desired for common use cases
Example:
The Thread class provides multiple constructors. For example:
Thread t = new Thread("MyThread");
If no arguments are required, the default constructor can be used. If a thread name is needed, a parameterized constructor is selected automatically.
Problem Without Constructor Overloading
Consider a class Box with only one constructor:
class Box {
double width, height, depth;
Box(double w, double h, double d) {
width = w;
height = h;
depth = d;
}
double volume() {
return width * height * depth;
}
}
In this case, the following statement is invalid:
Box box = new Box(); // Compile-time error
The class does not support creating:
- An empty box
- A cube using a single value
Example of Constructor Overloading
By adding multiple constructors, different initialization options become available.
class Box {
double width, height, depth;
Box(double w, double h, double d) {
width = w;
height = h;
depth = d;
}
// No-argument constructor
Box() {
width = height = depth = 0;
}
// Constructor for cube
Box(double len) {
width = height = depth = len;
}
double volume() {
return width * height * depth;
}
}
// Driver class
public class Test {
public static void main(String[] args) {
Box mybox1 = new Box(10, 20, 15);
Box mybox2 = new Box();
Box mycube = new Box(7);
System.out.println("Volume of mybox1: " + mybox1.volume());
System.out.println("Volume of mybox2: " + mybox2.volume());
System.out.println("Volume of mycube: " + mycube.volume());
}
}
Output
Volume of mybox1: 3000.0 Volume of mybox2: 0.0 Volume of mycube: 343.0
Using this() in Constructor Overloading
The this() keyword is used to call one constructor from another constructor in the same class. It helps avoid code duplication and ensures consistent initialization.
class Box {
double width, height, depth;
int boxNo;
Box(double w, double h, double d, int num) {
width = w;
height = h;
depth = d;
boxNo = num;
}
Box() {
width = height = depth = 0;
}
Box(int num) {
this(); // Calls the no-argument constructor
boxNo = num;
}
public static void main(String[] args) {
Box box1 = new Box(1);
System.out.println(box1.width);
}
}
Output
0.0
Rules for Using this() in Constructors
- Constructor calls must be the first statement in the constructor.
- Recursive constructor calls are not allowed.
- A constructor can call only one other constructor.
Invalid Example:
Box(int num) {
boxNo = num;
this(); // Compile-time error
}
Note:
- Constructors can be overloaded like methods, but they do not have return types.
- If any constructor is defined, Java does not generate a default constructor.
- Constructor overloading improves object initialization flexibility.
- Overloading is resolved at compile time.
Constructor Overloading vs Method Overloading
Feature | Constructor Overloading | Method Overloading |
|---|---|---|
Purpose | Initialize objects | Define multiple behaviors |
Return type | Not allowed | Required |
Name | Same as class | Any valid method name |
Invocation | new keyword | Method call |