Java constructor? What is a constructor?

 Java constructor

What is a constructor?

§  Constructor in java special type of method that is used to initialize the object.

§  Constructor name must be same as its class name.

§  Constructor  must have (no explicit return type)

Constructors are two types:-

1.       Default constructor

2.       Parameterized constructor

Example:-

Class Abc {

Abc   a= new Abc();

}

Abc()- is a  constructor

Example of default constructor:--

Class   ABC  
{

//no parameter //no  arguments

}

Example of Parameterized constructor:--

Class   ABC  
{

ABC(  int a,  int  y, String   z)  {   // its content argument

//body

}

}

Various senarious in java constructor:

Case1:--   when  no default constructor is provided.

Example:--              class Test   {
                       int   x;

                        float    y;

                       public static void main(String  args[])   {

                         Test   t= new  Test();

                         System.out.println(t.x);

                         System.out.println(t.y);

               }

}

Case2:--   when  no default constructor is provided  by the user.

Use where user wants to initialized the various with value other than java default values

Example:--      class Test   {
                       int   x;

                      Test();       {

                        x= 12;

                             }

                       public static void main(String  args[])   {

                         Test   t= new  Test();

                         System.out.println(t.x);

               }

}

Case3:--   when  no Parameterized constructor is provided  by the user(constructor with argument).

Example:--      class Test   {

                      Test(int  a,   String    b);       {

                        x= a;

                       y=b;

                             }

                       public static void main(String  args[])   {

                         Test   t= new  Test(12,  “programming khata”);

                         System.out.println(t.x);

                         System.out.println(t.y);

               }

}

Case4:--   constructor overloading (exactly  similar  to method overloading)

Example:--      class Test   {

                      Test(int  a,   String    b);       {

                        x= a;

                       y=b;

                             }

                       public static void main(String  args[])   {

                         Test   t= new  Test(12,  “programming khata”);    // this is Parameterized

                         Test   t= new  Test();                   //this is default

                         System.out.println(t.x);

                         System.out.println(t.y);

               }

}

Q.1  what will happen if only Parameterized is declared in the program and you try to call the default constructor?

Ans-    ERROR

Q.2  can you define a method with the same name as the constructor? 

Ans:-   Yes it is possible.

 


Post a Comment

0 Comments