Saturday, January 21, 2012

Constructor overloading in java allows to have more than one constructor inside one Class. in last article we havediscussed about method overloading and overriding and constructor overloading is not much different than methodoverloading. Just like in case of method overloading you have multiple methods with same name but differentsignature, in Constructor overloading you have multiple constructor with different signature with only differencethat 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 ?

When we talk about Constructor overloading, first question comes in mind is why do some one 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 lot of method overloading and constructor overloading. Constructor overloading make senseif 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 thatyou don't know size of arraylist during creation than 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 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 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 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 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 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.

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.