In Java, constructor overloading allows a class to have multiple constructors with different parameters. This enables us to create objects of the same class using different sets of input values. When a constructor is overloaded, each constructor can perform different initialization tasks depending on the arguments passed.
Syntax
public class ClassName {
// Constructors
public ClassName() {
// Default constructor code
}
public ClassName(type1 param1) {
// Constructor code
}
public ClassName(type2 param2) {
// Constructor code
}
// Additional constructors
// ...
}
Example
Let’s consider the example of a Person
class that represents a person’s information, such as their name and age. We can define different constructors to initialize the object based on different input parameters.
public class Person {
private String name;
private int age;
public Person() {
name = "Unknown";
age = 0;
}
public Person(String personName) {
name = personName;
age = 0;
}
public Person(String personName, int personAge) {
name = personName;
age = personAge;
}
// Other methods
//...
}
In the example above, the Person
class has three constructors:
- The default constructor sets
name
to “Unknown” andage
to 0. - The constructor with a single parameter
personName
setsname
to the specified value andage
to 0. - The constructor with two parameters
personName
andpersonAge
sets bothname
andage
to the specified values.
We can now create Person
objects based on the required parameters:
Person person1 = new Person(); // Using the default constructor
Person person2 = new Person("John"); // Using the constructor with a single parameter
Person person3 = new Person("Alice", 25); // Using the constructor with two parameters
By overloading the constructors, we provide flexibility to create objects with varying initializations depending on the requirements.
#Java #Constructors