Home Java Beginners Index Constructor overloading in java
Questions:Ask|Latest


 
 

Share on Google+Share on Google+

Constructor overloading in java

Advertisement
In this section we will discuss about constructor overloading in java. Constructor overloading is not much different from method overloading, in method overloading you have multiple method with same name having different signature but in case of constructor overloading you have multiple constructor with different signature.

Constructor overloading in java

In this section we will discuss about constructor overloading in java. Constructor overloading is not much different from method overloading, in method overloading you have multiple method with same name having different signature but in case of constructor overloading you have multiple constructor with different signature, only one difference is there constructor doesn't have return type in java. How to overload a constructor, in java you need to create another constructor with different signature but same name as class name. Now here is a example to show how constructor are overloaded.

public class Demo
{
      public Demo(int a)
     {
       //Statement
      }
     public Demo(int a,int b)
     {
        //statement;
     }
     public Demo(int a, int b, int c)
     {
       //statement;
      }
   } 

While working with constructor overloading some important points to remember, they are as follows:

  • You can call overloaded constructor using this() keyword.
  • Overloaded constructor can be called from another constructor.
  • Compiler will automatically create a default constructor while creating object of the class.
  • While calling constructor from another constructor this should be the first statement in the constructor.
Example : A program for constructor overloading
public class Demo 
{
	 public Demo()
	 {
		 System.out.println("In default constructor");
	 }
                 public  Demo(int i)
                   {
       	
    	System.out.println("value of i = "+i);
    	System.out.println("In parameterized constructor");
                    }
   
                    public Demo(int a,int b)
                   {
    	
                  int c=a+b;
    	System.out.println("value of c = "+c);
                   System.out.println("In  another parameterized constructor");
 
                     }
                 public static void main(String args[])
                   {
	   Demo ob=new Demo();
	   Demo ob1=new Demo(10);
	   Demo ob2=new Demo(10,15);
	   System.out.println("In main method");
                     }
    }

Output: After compiling and executing of above program

Download SourceCode

Advertisements

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: May 14, 2013

Ask Questions?    Discuss: Constructor overloading in java  

Post your Comment


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