❮ Previous Next ❯

Java Copy Constructor

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

❮ Previous Next ❯


  Tutorials

Popular Links

Contact Us

address Plot No-741,2ND Floor
Opp. Bhagabati Temple,Jayadev Vihar
      Bhubaneswar-751013
      Email: info@silantechnology.com
      Phone: 0674-2361252
address
39877, sundale dr, apt#101, Fremont, California 94538, USA
       Phone: +1(262)388-7619

facebook twitter google linked in

© 2018 Silan Technology. All Rights Reserved