What is Constructor overloading in Java with Example

Constructor overloading in java allows having more than one constructor inside one Class. in the last article we have discussed method overloading and overriding and constructor, overloading is not much different than method overloading. Just like in the case of method overloading you have multiple methods with the same name but different signature, in Constructor overloading you have multiple constructors with a different signature with the only difference that 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 ?

Constructor overloading in java with ExampleWhen we talk about Constructor overloading, the first question comes to mind is why do someone overload Constructors in Java or why do we have overloaded constructor ? If you have been using framework or API like JDK or Spring you must have seen a lot of method overloading and constructor overloading. Constructor overloading makes sense if 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 that you don't know the size of ArrayList during creation then you can simply use default no argument constructor but if you know size then its best to use overloaded Constructor which takes capacity. 

Since ArrayList can also be created from another Collection, may be from another List than having another overloaded constructor makes lot of sense. 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 be remembered 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 will throw 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 added any 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.


Java post you may like


Anand VijayaKumar said...

Nice post Javin. More details on How to create and use constructors,their purpose etc can be found by clicking here.


Anonymous said...

I understand that we can overload Constructor in Java but is it possible to override Constructor in Java ? does declaring constructor on sub class actually override super class constructor in Java.

Anonymous said...

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.

Post a Comment