Constructor Overloading in Java | with Examples

Let us explore what does it mean to do a constructor overloading in Java. We will explore the constructor of Java with some pretty neat code examples.

Before we dig right into the topic let us quickly revise a constructor.

What is a constructor in Java ?

A constructor in Java is a special method that is doesn’t behave like a normal method. It doesn’t have a return type defined in its declaration. And it’s name must be exactly as the name of the class.

If we look at a normal method. It’s main purpose is to perform some action in its own instance. But a constructor’s primary function is entirely different. Having said that, you cannot use constructor as a regular method on it’s resulting instance.

So, then what exactly is a constructor ?

Are you are familiar with the new keyword in Java ? If you answered yes, then you are close to understanding what a constructor is.

The constructor is invoked whenever you create an object of a class. Yes, that is whenever you do a new YourClassName();. In other words it is used to initialize the instance of an object.

Let us look at a simple code snippet.

public class Person() { public Person() { // 2. this is the constructor in Java. } } // 1. creating a new instance of the class above Person p = new Person();
Code language: Java (java)

In the above example when we create a new Person() the constructor method is invoked. And that sums up our constructor revisit.

Now let us move to overloading the constructor.

What is constructor overloading in Java ?

Constructor overloading in Java can be defined as a way of having more than one constructor in a class. We can create many constructors with different parameters.

This help us initialize the object in a number of different ways. Le us look at the below class for example ( read the comments ).

public class Person() { private int age; private String firstName; private String lastName; private String occupation; public Person(String firstName, String lastName, int age, String occupation) { // Constructor No. 1 this.firstName = firstName; this.lastName = lastName; this.age = age; this.occupation = occupation; } public Person(String name, int age) { // Constructor No. 2 // we can overload constructor by only accepting required arguments // in case of this example we will accept only 2 arguments // and we will fill in other details here without any issue firstName = name; this.age = age; lastName = ""; // fill in empty last name occupation = "Unemployed"; // set desired occupation } public Person(String name) { // Constructor No. 3 // we can also add another constructor that accepts only one argument // let us supply only the name of the person and fill in other details here. firstName = name; lastName = ""; // empty occupation = "Student"; // everyone is studying age = 20; // every person object created with this constructor will be 20 year old } }
Code language: JavaScript (javascript)

As we can see that in the class above we have in total 3 constructors. In other words this is known as constructor overloading.

And we have 3 constructor overloads. Next let us understand why do we require constructor overloading in Java.

Why use constructor overloading in Java ?

Now that we have seen how we do constructor overloading. Let us understand why do we need constructor overloading in Java.

We use constructor overloading approach in Java to initialize the object in multiple different ways. It adds some flexibility to the object construction.

Let’s say you need to set the values while the object is being created like in the examples explained below. Then constructor overloading is a desirable approach.

As we can see in the code example above. We have multiple constructors initializing the object in multiple ways.

For instance let us take an example of Constructor No. 3. It’s obvious from the constructor that the resulting object is a Student.

And Constructor No. 2 is used when we want to create a person who is Unemployed.

So that was a quick explanation about constructor overloading. Keep learning and coding.


Java List Data Structure: Learn Real Use Case Of List

ArrayList in Java: Understand With Example

Run Length Encoding in Java: Learn How To Implement

Interface in OOP: Guide to Polymorphism and Callbacks

Related Posts