Constructor Overloading

In our earlier post we learnt about method overloading.Today we shall apply that technique to overload constructors.A method is said to be constructor if the name of the method and the name of the class where it presents is same.During the declaration of the object some times we need to declare different types of object.One may take one parameter as input, other may take two parameters of as inputs,it is dependent of the user.Therefore we need to write to different constructors to initialize different types of objects.
The demo program which follows constructor overloading is:

import java.util.Scanner;
class construover
    int x;
    double y;
    construover()                              //default consturctor with zero parameters.
    construover(int a)                     //Constructor with one parameter.
    construover(int a,double b)   //Constructor with two parameters.
     void display()
               System.out.println("X value is:" + x);
               System.out.println("y value is:" + y);
class construoverdemo
   public static void main(String arg[])
         construover ob = new construover();                         //Invokes default constructor.
         construover ob1 = new construover(10);           //Invokes constructor with one parameter.
         construover ob2 = new construover(10,5.35);   //Invokes constructor with one parameter.
         System.out.println("Variables invoked through single parameter consturctor"); 
          System.out.println("Variables invoked through double parameter consturctor");                   

The output of the above program is:
You can download the program.


About Anuroop D

Very enthusiastic about technology and likes to share my knowledge through blogging. Has Bachelor's in Information Technology and currently pursuing my Master's in Computer Science.
This entry was posted in Java and tagged , , , , , , , , , , , , , , , , , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s