Monday 30 January 2017

Constructors in Java


                                                     
Constructors in Java
 

Constructor

Constructor is a special member function that has same name as that of class name. A constructor initializes an object immediately upon creation.

It has same name as that of class name in which it resides and is syntactically similar to method. It is automatically called when the objects of the class are instantiated.

Its does not has any return type , not even void. This is because , the implicit return type of a class' constructor is the class type itself.

Syntax-
   
                  class class_name {
         
                     class_name ( parameters list )          // constructor
                        {
                        // body of constructor
                         }
                    .
                    . 
                    .
                  }

 Types of Constructor
  
       There are 3 types of constructor :

          - Default constructor
          - Parameterized constructor
          - Default Constructor

    Default Constructor

 The default constructor does not has any arguments passed with it.

Program:

                  class BoxDemo
            {
                    BoxDemo ( )
                 {
                       int a=10 ;
                      int b= 20 ;
                  }
               
                void display ()
              {
               System.out.println (" Area of box =" + (a*b));
               }
          }
            class DefaultConst 
       {
           public static void main ( String args [] )
              {
                  BoxDemo b = new BoxDemo ( );
                      b.display () ;
                 }
       }
                                                              Output-  Area of box = 200

    Parameterized Constructor

Parameterized constructor has the parameters passed with it when the objects are created using new operator.

Program:

         class BoxDemo
        {
                 int length ;
                 int breadth ;
                  
                  BoxDemo ( int l , int b )
              {
                       length=l ;
                       breadth = b;
              }
              
                 void display ( )
                 {
            System.out.println( " Area of Box= "+ ( length * breadth ));
             }
        }
         class ParamDemo
     {
         public static void main ( String args [] )
        {
           BoxDemo b = new BoxDemo (10,20 );
             b.display ( );
         }
    }
                                                                Output: Area of box=200

        Copy Constructor

 In copy constructor , a copy of constructor is created which takes the object of the original constructor as arguments.

 It is used when you do not want to alter the original contents of the constructor.

Program:

     class BoxDemo
    {
                   int length ;
                   int breadth ;
          BoxDemo ( int a , int b )
        {
               length = a;
               breadth = b;
         }
        BoxDemo ( BoxDemo obj )
      {
           System.out.println (" Copy Constructor Invoked " );
          length = obj.length ;
          breadth= obj.breadth ;
     }
         int area ()
    {
         return ( length * breadth ) ;
     }
 }
  class CopyDemo 
 {
        public static void main ( String [] args )
  {
      BoxDemo b1 = new BoxDemo ( 10,20) ;
      BoxDemo b2 = new BoxDemo ( b1 ) ;
   System.out.println ( " The area is = " + b1.area () ) ;
   System.out.println ( " The area is = " + b2.area () ) ;
}
}
                                                          Output:  The area is = 200
                                                               Copy Constructor invoked
                                                                          The area is = 200

this keyword

 this keyword can be used inside any method to refer to the current object , i.e , this is always a reference to the object on which the method was invoked.

 Ex-
     // To demonstrate this
     Box ( double w , double h , double d )
   {
       this.width= w ;
       this.height= h ;
       this.depth = d ;
  }


                                                                        By: Knowledge Bits 

No comments:

Post a Comment

Video of the Day

Contact us

Name

Email *

Message *