Home Java Beginners Constructor Overloading in Java
Questions:Ask|Latest

 
 

Share on Google+Share on Google+

Constructor Overloading in Java

Advertisement
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:

Advertisement

If you enjoyed this post then why not add us on Google+? Add us to your Circles



Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: October 9, 2013

Related Tutorials

Ask Questions?    Discuss: Constructor Overloading in Java  

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments
DMCA.com