Constructor overloading in Java
Properties and methods make Java classes interesting.
Properties represent the data an object possesses, while methods enable the intelligent manipulation of that data. However, to perform any meaningful operations with the data a class contains, the properties must be initialized to meaningful values.
Initializing the properties of a class is the job of a Java constructor. When a class has a variety of properties to initialize, developers often provide a set of overloaded Java constructors to initialize those properties in many different and meaningful ways.
What is constructor overloading in Java?
Constructor overloading in Java occurs when a class has multiple constructors, each with a separate and unique method signature. Overloading constructors in Java provides a variety of benefits to both the component developer and the API user:
- The ability to set sensible defaults when property values are unknown.
- The ability for the API user to perform custom initializations on known properties.
- Backwards compatibility to support new properties.
- Simple and flexible object creation to match the needs of API users.
- Opportunities for code reusability, leading to enhanced maintainability.
Java constructor overloading example
Imagine a simple Java class that represents a point on a Cartesian plane. The class has two properties: x and y. The following code is an example.
public class Point {
int x;
int y;
}
For a user of this class to set the x and y value of Point, you must provide a constructor that takes two arguments.
public class Point {
int x;
int y;
Point(int a, int b){
x = a;
y = b;
}
}
The following code shows how to write the code to create a point at Coordinates 4 and 2.
Point p1 = new Point(4,2);
Overloading with the zero-argument constructor
However, it might also be common in the application to create an origin point at coordinates (0,0). In this case, the developer might provide a zero-argument constructor that lets a developer create a point with 0, 0 as the defaults.
Point() {
x = 0;
y = 0;
}
Overloading with more parameterized constructor
Furthermore, you might need a Point where x and y have the same value. To perform this task, we could provide a constructor that takes only one value that is assigned to both x and y.
Point(int a){
x = a;
y = a;
}
When to use overloaded constructors
As you can see, it can make sense to perform constructor overloading even in a simple Java class that that has only two properties. For more complex classes, it's not uncommon to have five or more highly parameterized overloaded constructors.
A developer that uses the Point class could use any of the overloaded constructors where they make sense. The following code shows the use of each of these three constructors.
Point origin = new Point(); //creates a point at 0,0
Point oneone = new Point(1); //creates a point at 1,1
Point four20 = new Point(4, 20); //creates a point at 4,20
The Point class
The full code for the Point class is as follows.
public class Point {
int x;
int y;
Point() {
x = 0;
y = 0;
}
Point(int a) {
x = a;
y = a;
}
Point(int a, int b) {
x = a;
y = b;
}
}
Optimizing overloaded Java constructors
These overloaded constructors repeat the same basic initialization steps, and that's a code smell.
If you want an overloaded Java constructor to call another overloaded constructor, you can use the this() method. With it, you can highly optimize the previous code.
public class Point {
int x;
int y;
Point() { this(0, 0); }
Point(int a) { this(a, a); }
Point(int a, int b) {
x = a;
y = b;
}
}
Constructors are one of the most important constructs in the Java programming language. The ability to overload constructors in Java makes your APIs easier to use and easier to maintain in the long term.
Cameron McKenzie has been a Java EE software engineer for 20 years. His current specialties include Agile development; DevOps; and container-based technologies such as Docker, Swarm and Kubernetes.