Constructor Overloading in Java

Java


In this article, we show how do constructor overloading in Java.

Constructor overloading means that you create more than 1 constructor for a class. That's all that is meant by constructor overloading.

Overloading may seem like a negative term, since it seems like you're doing something over the amount that is supposed to be done, but it's just really a term used in Java. Constructor overloading in Java is a fairly common thing to do and causes no problems in a program.

As long as each constructor created has a different set of parameters, then it's perfectly fine to do.

One reason to perform constructor overloading in a Java program is because certain parameters passed into an object may be optional.

Let's say for instance that we have a class of students where each object created represents a student.

In one constructor, we pass in the student's name and age.

Now let's say we have an optional parameter where a student can enter his or her race in addition to his or her name and age.

So if the student chooses to display his or her race, we have another constructor in the program that assigns all 3 of these properties. This time, the constructor accepts 3 parameters, the name, age, and race of the student.

So we've just created 2 constructors for this students class. One that accepts just the name and age of a student. And another that accepts the name, age, and race of the student. In other words, we have overloaded constructors for this class.

So the example code is a prime example of when we may have overloaded constructors for a class.

If you have objects where you may have optional properties to be assigned to the object based on whatever circumstances there is, you may want to do constructor overloading for this class. This way, you assign variables to objects based on whatever information you have on an object, and it may be different for each object.

So below we create a complete class and then have a test class below where we overload constructors.

It's the same example spoken of above, where we have a students class and one constructor accepts just the name and age. And another constructor accepts the name, age, and race of the student.



So we create a class named Students.

In this class, we declare 3 variables that will function as properties for student objects that are created. These are a String named name, an int named age, and a String named race. These represent the name, age, and race of a student. We set the race variable equal to "unknown" initially because this is seen as the optional parameter. If the parameter is then passed into the object, this "unknown" value gets overridden. If this property isn't passed into the object, then the value stays unknown.

Below this we create a constructor. Remember that a constructor must have the same name as the class. Since our class is Students.java, our constructor must be named Students as well.

The first constructor takes in 2 parameters, the name and age of the student, which can be seen as the mandatory data. We must pass in the name and age of a student.

We then create a second constructor. This time, it has 3 parameters. It takes in the name, age, and race of a student. If the student's race is known, then we will pass in the race.

So this program has constructor overloading.

Next, we write the test class so that we can run this program.

This is shown below.



Java Output

Michelle is 12 years old and the student's race is unknown
Peter is 13 years old and the student's race is Black


So this is the class that tests our overloaded constructor program.

In this class, we have a main method because we want the execution of the project to be done at this main method.

We create 2 objects of the Students class.

We create the student1 object, where we pass in 2 parameters, the name and age. In the first object, the name is Michelle and the age is 12. This automatically calls the first constructor in the Students class, that has 2 parameters.

We then create a student2 object, where we pass in 3 parameters, the name, age, and race of the student. In the second object, the name is Peter, the age is 12, and the race is black. This object instantiation calls the second constructor in the Students class, that has 3 parameters.

We then output the properties of the objects.

Since the first object only has the name and age property input, the race variable stays "unknown" and that is what is output.

The second object has the name, age, and race property input. So the "unknown" race value gets overridden and the new value for race is now "Black".

So this is how a program with constructor overloading works. It's very commonly used. It's appropriate to overload a constructor in code. And you can do the same thing in Java with methods, called method overloading.


HTML Comment Box is loading comments...