Pages

Constructor overloading in java allows having more than one constructor inside one Class. in the last article we havediscussed method overloading and overriding and constructor, overloading is not much different than methodoverloading. Just like in the case of method overloading you have multiple methods with the same name but differentsignature, in Constructor overloading you have multiple constructors with a different signature with the only differencethat Constructor doesn't have a return type in Java. That constructor will be called as an overloaded constructor . Overloading is also another form of polymorphism in Java which allows having multiple constructors with a different name in one Class in java.

Why do you overload Constructor in Java ?

When we talk about Constructor overloading, the first question comes to mind is why do someone overload Constructorsin Java or why do we have overloaded constructor ? If you have been using framework or API like JDK or Springyou must have seen a lot of method overloading and constructor overloading. Constructor overloading makes senseif you can Construct object via a different way. One of Classical example of Constructor overloading is ArrayList in Java. ArrayList has three constructors one is empty, other takes a collection object and one takes initial Capacity. these overloaded constructors allow flexibility while creating an ArrayList object. It may be possible thatyou don't know the size of ArrayList during creation then you can simply use default no argument constructor but ifyou know size then its best to use overloaded Constructor which takes capacity.

Since ArrayList can also becreated from another Collection, may be from another List than having another overloaded constructor makes lot ofsense. By using overloaded constructor you can convert your ArrayList into Set or any other collection.

Constructor overloading in Java Example

How to overload Constructor in Java

Constructor overloading is not complex you just need to create another constructor, obviously same name as of class but different signature but there are certain rules related to Constructor overloading which needs to beremembered while overloading constructor in Java. e.g. One Constructor can only be called from inside of another Constructor and if called it must be the first statement of that Constructor. here is an example of correct and incorrect constructor overloading:

public loan(){

this(""); //correct

}

public loan(){

System.out.println("Calling overloaded Constructor in Java");

this(""); //incorrect - throw compilation error.

}

public loan(String type){

this.loanType= type;

}

Also, once you provide constructor on Class in Java , Compiler will not insert or add default no argument constructor , so make sure you add a default constructor in your class. Other rules of method overloading

also apply on Constructor overloading in Java e.g. Number or type of arguments of constructor should be different. Just changing access modifier of Constructor will not result in overloading instead it willthrow compilation error as shown in below example:

public loan(String type){

this.loanType= type;

}

//compilation error - only access modifier is changed

private loan(String type){

this.loanType= type;

}

Important points related to Constructor overloading:

1. Constructor overloading is similar to method overloading in Java.

2. You can call overloaded constructor by using this() keyword in Java.

3. overloaded constructor must be called from another constructor only.

4. make sure you add no argument default constructor because once compiler will not add if you have addedany constructor in Java.

5. if an overloaded constructor called , it must be the first statement of constructor in java.

6. Its best practice to have one primary constructor and let overloaded constructor calls that. this way

your initialization code will be centralized and easier to test and maintain.

That’s all on Constructor overloading in java. The biggest advantage of Constructor overloading is flexibility which allows you to create the object in a different way and classic examples are various Collection classes. Though you should remember that once you add a constructor, a compiler will not add default no argument constructor.

Can you overload Constructor in Java was asked in one of Interview, I think on Tech Mahindra. Well, Yes, Certainly. then they ask how to do you explicitly call overloaded constructor in Java, Answer is using this() keyword. You can also use super() to call constructor from super class.

Hello @Anonymous, the most important function of constructor overloading is to allow create object without all details. Since object has both mandatory and optional details, by overloading constructor you can segregate them e.g. new Person() is not ok, because you have to give a name but phone can be optional. So new Person(String name) and new Person(String name, Long Phone) will work fine.