Logo 
Search:

Java Articles

Submit Article
Home » Articles » Java » FundamentalRSS Feeds

Constructor Overloading

Posted By: Sherman Wood     Category: Java     Views: 14975

This article explains about overloaded constructors in java with examples.

A class can have several constructors. This feature of java is called constructor overloading.

Note that all constructor of a class must have different parameter list i.e the signature of each constructor must differ.

The signature of a constructor is a combination of its name and the sequence of its parameter types.

Overloaded constructors are very common in java because they provide several ways to create an object of a particular class. It also allow you to create and initialize an object by using different types of data.

Syntax of Overloaded Constructor

class clsName
{
        // Variable declaration

        // Constructor
        clsName1()
        {
            // body of constructor
        }
        
        clsName2(cparams2)
        {
            // body of constructor
        }
  :
  :
        
        clsNameN(cparamsN)
        {
             // body of constructor
        }

        // Methods
}


Example of Overloaded Constructor

Example 1 : Program to create a class named as Square having variables height and width, overloaded constructors assigning value to variables

class Square
{
       int height;
       int width;

       Square()
       {
               height = 0;
               width = 0;
        }
        Square(int side)
        {
               height = width = side;
         }
         
         Square(int sideh, int sidew)
         {
                height = sideh;
                width = sidew;
          }
}

class ImplSquare
{
     public static void main(String args[])
     {
             Square sObj1 = new Square();
             Square sObj2 = new Square(5);             
             Square sObj3 = new Square(2,3);             

             System.out.println("Variable values of object1 : ");
             System.out.println("Object1 height =  " + sObj1.height);
             System.out.println("Object1 width = " + sObj1.width);
             System.out.println("");

             System.out.println("Variable values of object2 : ");
             System.out.println("Object2 height =  " + sObj2.height);
             System.out.println("Object2 width = " + sObj2.width);
             System.out.println("");

             System.out.println("Variable values of object3 : ");
             System.out.println("Object3 height =  " + sObj3.height);
             System.out.println("Object3 width = " + sObj3.width);
      }
}


Output

Variable values of object1 : 
Object1 height =  0
Object1 width =  0

Variable values of object2 : 
Object height =  5
Object width =  5

Variable values of object3 : 
Object height =  2
Object width =  3
  
Share: 


Didn't find what you were looking for? Find more on Constructor Overloading Or get search suggestion and latest updates.

Sherman Wood
Sherman Wood author of Constructor Overloading is from New York, United States.
 
View All Articles

 
Please enter your Comment

  • Comment should be atleast 30 Characters.
  • Please put code inside [Code] your code [/Code].

 
No Comment Found, Be the First to post comment!