Here we have discussed about Constructor Overloading in Java with a simple example.
In this section we have discussed about Constructor Overloading in Java.
Constructors are special methods that are used to assign initial values to instance variables of the class that have no return type. Constructor are declared like methods, their name are same as class name. Constructor called by new operator. A default constructor however is automatically called by the Java Virtual Machine (JVM).
Constructor contains different types and number of parameters and are used to initialize the values of variables. Constructors can be only overloaded and not overrided.
Different arguments must be passed in every constructor as JVM differentiates based on arguments and then overloads them.
Following example of Constructor Overloading will help you understand the concept even more:
Syntax:
public class className
{
// constructor definition
// constructor name is same as of class name
public className(dataType1 variableName1)
{
//Statement
}
public className(dataType1 variableName1, dataType1 variableName2)
{
//statement;
}
public className(dataType1 variableName1, dataType1 variableName2, dataType1 variableName3)
{
//statement;
}
public className(dataType2 variableName1, dataType3 variableName2)
{
//statement;
}
}
Example:
Here we will see how the constructors are overloaded. We have created a Java class where various constructors with different number of parameters are created. Then we have used this constructors to initialize the values of instance variables.
ConstructorOverloadingExample.java
public class ConstructorOverloadingExample {
int i,j,k;
String str1, str2, str3;
public ConstructorOverloadingExample()
{
System.out.println("Default Constructor");
}
public ConstructorOverloadingExample(int i)
{
this.i= i;
System.out.println("In One Parameterized Constructor i is : "+i);
}
public ConstructorOverloadingExample(int i, int j)
{
this.i= i;
this.j= j;
System.out.println("In Two Parameterized Constructor i and j are : "+i+" and "+j);
}
public ConstructorOverloadingExample(int i, String str1)
{
this.i= i;
this.str1= str1;
System.out.println("In Two Parameterized Constructor i and str1 are : "+i+" and "+str1);
}
public ConstructorOverloadingExample(String str1, String str2)
{
this.str1= str1;
this.str2= str2;
System.out.println("In Two Parameterized Constructor str1 and str2 are : "+str1+" and "+str2);
}
public ConstructorOverloadingExample(String str3, int k)
{
this.str3= str3;
this.k= k;
System.out.println("In Two Parameterized Constructor str3 and k are : "+str3+" and "+k);
}
public static void main(String args[])
{
ConstructorOverloadingExample defaultconstructor = new ConstructorOverloadingExample();
ConstructorOverloadingExample constructor1 = new ConstructorOverloadingExample(5);
ConstructorOverloadingExample constructor2 = new ConstructorOverloadingExample(5, 10);
ConstructorOverloadingExample constructor3 = new ConstructorOverloadingExample(5, "Roseindia");
ConstructorOverloadingExample constructor4 = new ConstructorOverloadingExample("Roseindia", ".net");
ConstructorOverloadingExample constructor5 = new ConstructorOverloadingExample("Roseindia", 10);
}
}
Resource: