The constructor which is used to copy one object into another object, is known as copy constructor. Actually it is the constructor in C++, not in java. Since java supports all C++ features, so this concept is supported.
 

Example3;

package java8s;

 

class A
{
      int x,y;
      A(int p, int q)    //parameterized constructor
      {
            x=p;
            y=q;
      }
      A(A r)            // copy constructor
      {
            x=r.x;
            y=r.y;
      }
      void show()
      {
            System.out.println("x="+x);
            System.out.println("y="+y);
      }
}
class Example1 {

      public static void main(String[] args) {
            A ob1=new A(100,200);
            A ob2=new A(ob1);
            System.out.println("in ob1");
            ob1.show();
            System.out.println("in ob2");
            ob2.show();
           

      }

}

 

 

 

Output


copy-constructor

Our Services:

Java Training | Python Training | Machine Learning with Python Training | Deep Learning with Python Training | Projects

SILAN Technology
Call to: 9439202111  |  9658317757  |  0674-2361252

Comment