Page 303 - CA_Blue( J )_Class10
P. 303

14      void display ()
                  15      {      System.out.println("Area of Rectangle: " + area);

                  16          System.out.println("Perimeter of Rectangle: " + perimeter);
                  17      }

                  18
                  19      public static void main()

                  20      {
                  21          rectangle ob = new rectangle();

                  22          ob.cal();
                  23          ob.display();

                  24      }
                  25  }

                 You will get the following output:












                 12.2.4 Copy Constructor
                 The copy constructor is a special type of constructor that is used to create an object by initializing it with previously
                 created object of the same class. Thus, in one word it duplicates an object. The different ways to initialize the instance
                 variables of one object into another in Java are as follows:
                 •  By using a constructor
                 •  By assigning the values of one object into another

                 By Using a Constructor
                 Here, since object is passed as a parameter, instance variables of the object are copied to the instance variable of the
                 current object.

                   1  class rectangle2

                   2  {
                   3      int length, breadth, area, perimeter;
                   4      rectangle2 (int l, int b)

                   5      {

                   6         length=l;
                   7         breadth=b;

                   8      }






                                                                                                                       301
                                                                                                         Constructors  301
   298   299   300   301   302   303   304   305   306   307   308