Home Java Beginners Java Constructor Overloading Example

 
 

Share on Google+Share on Google+

Java Constructor Overloading Example

Advertisement
Constructors in a class with the different parameters are known as constructor overloading in Java programming.

Java Constructor Overloading Example

In this section we will read about the constructor overloading in Java. We will see how the constructor overloading is achieved in the Java programming.

Constructors in Java are a special type of methods that have no any return type. Constructor's name is same as of its class name, it may contain any number of parameters. Constructors defined with the different type of parameters and different numbers of parameters in Java programming is called constructor overloading. Constructors with different parameters and number of parameters are used to initialize the values of variables. As we see earlier that a constructor in Java are the special type of methods that applies the Polymorphism feature in programming code using overloading and overriding but, in case of constructors, these can be only overloaded.

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 an example is being given which will demonstrate you how the constructors are overloaded. In this example we will create a Java class where we will create the various constructors with different number of parameters and or different types of parameters. Then we will use 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);		
	}
}

Output

When you will compile and execute the above Java class you will get the output as follows :

Download Source Code

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: August 13, 2013

Related Tutorials

Discuss: Java Constructor Overloading Example  

Post your Comment


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