What is Constructor overloading in Java with Example

Constructor overloading in java allows to have more than one constructor inside one Class. in last article we have discussed about method overloading and overriding and constructor overloading is not much different than method overloading. Just like in case of method overloading you have multiple methods with same name but different signature, in Constructor overloading you have multiple constructor with different signature with only difference that Constructor doesn't have return type in Java. Those constructor will be called as overloaded constructor . Overloading is also another form of polymorphism in Java which allows to have multiple constructor with 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, first question comes in mind is why do some one 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 lot of method overloading and constructor overloading. Constructor overloading make sense if you can Construct object via 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 take initial Capacity. these overloaded constructor allows flexibility while create arraylist object. It may be possible that you don't know size of arraylist during creation than 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 converty 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 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 on your class. Other rules of method overloading
also applies 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 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. Biggest advantage of Constructor overloading is flexibility which allows you to create object in different way and classic examples are various Collection classes. Though you should remember that once you add a constructor, 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