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 signatures, 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.
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 Constructors in Java?
When we talk about Constructor overloading, the first question that comes to mind is why does someone overload Constructors in Java or why do we have overloaded constructors? If you have been using a 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 objects in a different way.
One of Classical example of Constructor overloading is ArrayList in Java. ArrayList has three constructors one is empty, the 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 it's best to use overloaded Constructor which takes capacity.
Since ArrayList can also be created from another Collection, maybe from another List than having another overloaded constructor makes a lot of sense. By using an overloaded constructor you can convert your ArrayList into Set or any other collection.
One of Classical example of Constructor overloading is ArrayList in Java. ArrayList has three constructors one is empty, the 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 it's best to use overloaded Constructor which takes capacity.
Since ArrayList can also be created from another Collection, maybe from another List than having another overloaded constructor makes a lot of sense. By using an overloaded constructor you can convert your ArrayList into Set or any other collection.
Constructor overloading in Java Example
Constructor overloading is not complex you just need to create another constructor, the obviously the same name as of class but different signature but there are certain rules related to Constructor overloading which need 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:
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 a constructor on Class in Java, the 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 to Constructor overloading in Java e.g. Number or type of arguments of the constructor should be different.
Just changing the access modifier of Constructor will not result in overloading instead it will throw compilation error as shown in below example:
Just changing the 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 an 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.
Thanks
Java post you may like
8 comments :
Nice post Javin. More details on How to create and use constructors,their purpose etc can be found by clicking here.
Anand
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.
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.
Can we say String class also example for constructor overloading?
what is the function of constructor overloading?
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.
I need a proper definition of constructor overloading with example ????
The below statement in last paragraph has error: Instead of different name it should be different signature.
Overloading is also another form of polymorphism in Java which allows having multiple constructors with a different name in one Class in java
Post a Comment