Understanding Copy Constructors in Java with Examples

06 May 2023 Balmiki Mandal 0 Core Java

Copy Constructor in Java with Examples

A copy constructor is a type of constructor. It is used to create an object that is a copy of another object. A copy constructor has the same name as the class and takes a single parameter of type const reference to the same class.

It creates the new object by copying the content of the given object into the new object using memberwise copying. The copy constructor can be used to create a deep copy or a shallow copy depending on the needs.

Example 1: Simple Copy Constructor

In this example, we have a simple class called MyClass that has one int data member. The class has a copy constructor and a simple display() member function.


//Simple copy constructor
class MyClass
{
    int x;
    public:
    //default constructor
    MyClass()
    { 
       x = 0; 
    }
 
    //copy constructor
    MyClass(const MyClass &obj)
    {
       x = obj.x; 
    }
 
    void display()
    {
       cout << "x = " << x << endl;
    }
 };

The copy constructor simply copies the content of the given object into the newly created object.

Example 2: Shallow Copy Constructor

In this example, we have a class called MyClass2. It has two int members and a pointer to an int data. The class also has a shallow copy constructor.


//Shallow copy constructor
class MyClass2
{
   int x;
   int y;
   int *ptr; 
 
public:
   //parameterized constructor
   MyClass2(int x1, int y1, int z1)
   {
      x = x1;
      y = y1;
      ptr = new int;
      *ptr = z1;
   }
 
   //copy constructor - shallow copy
   MyClass2(const MyClass2 &obj)
   {
      x = obj.x;
      y = obj.y;
      ptr = obj.ptr; //pointer variable just gets copied
   }
 
   void display()
   {
      cout << "x = " << x << ", y = " << y << ", *ptr = " << *ptr << endl;
   }
};

The shallow copy constructor copies the x and y data members but it just copies the memory address of the pointer data member from the source object to the target object. That is why it is called a shallow copy.

Example 3: Deep Copy Constructor

In this example, we have a class called MyClass3. It has two int members and a pointer to an int data. The class also has a deep copy constructor.


//Deep copy constructor
class MyClass3
{
   int x;
   int y;
   int *ptr; 
 
public:
   //parameterized constructor
   MyClass3(int x1, int y1, int z1)
   {
      x = x1;
      y = y1;
      ptr = new int;
      *ptr = z1;
   }
 
   //copy constructor - deep copy
   MyClass3(const MyClass3 &obj)
   {
      x = obj.x;
      y = obj.y;
      ptr = new int;
      *ptr = *(obj.ptr); //allocate a new memory to the pointer and copy data
   }
 
   void display()
   {
      cout << "x = " << x << ", y = " << y << ", *ptr = " << *ptr << endl;
   }
};

The deep copy constructor allocates a new memory for the pointer variable and copies the content of the source pointer variable into it. This creates a deep copy of the pointer variable as well. That is why it is called a deep copy.

Conclusion

Copy constructor is a type of constructor. It creates a new object by copying the content of the given object into the new object. It can be used to create a shallow copy or a deep copy depending on the needs.

BY: Balmiki Mandal

Related Blogs

Post Comments.

Login to Post a Comment

No comments yet, Be the first to comment.