5 Difference Between Shallow And Deep Copy of a Class

Table of Contents

What is a Shallow Copy?

shallow copy of an object is a copy whose properties share the same references (point to the same underlying values) as those of the source object from which the copy was made.

In shallow copy, an object is created by simply copying the data of all variables of the original object. This works well if none of the variables of the object are defined in the heap section of memory. If some variables are dynamically allocated memory from heap section, then copied object variable will also reference then same memory location.

This will create ambiguity and run-time errors dangling pointer. Since both objects will reference to the same memory location, then change made by one will reflect those change in another object as well. Since we wanted to create a replica of the object, this purpose will not be filled by Shallow copy. 

Characteristics

  • Shallow copy stores the reference of objects to the original memory address.
  • Shallow copy is faster.
  • Shallow copy reflects changes made to new/copied object in the original object.
  • Shallow copy stores the copy of the original object and points the references to the objects.
  • Shallow copy is less expensive.  

What is Deep Copy?

A deep copy of an object is a copy whose properties do not share the same references (point to the same underlying values) as those of the source object from which the copy was made. Deep copy is used in scenarios where a new copy (clone) is created without any reference to original data.

As a result, when you change either the source or the copy, you can be assured you’re not causing the other object to change too; that is, you won’t unintentionally be causing changes to the source or copy that you don’t expect. That behavior contrasts with the behavior of a shallow copy, in which changes to either the source or the copy may also cause the other object to change too (because the two objects share the same references).

Deep copy can be illustrated with an example by considering an Employee object having AddressInfo as a member of reference type along with other members of value type. A deep copy of Employee creates a new object, Employee2, with members of value type equal to Employee but references a new object, AddressInfo2 , which is a copy of AddressInfo.

Characteristics

  • Deep copy stores copies of the object’s value.
  • Deep copy is comparatively slower.
  • Deep copy doesn’t reflect changes made to new/copied object in the original object.
  • Deep copy stores the copy of the original object and recursively copies the objects as well.
  • Deep copy is expensive as compared to shallow copy in terms of object creation, because it involves recursive copying of data from other mutable objects, which is part of original object.

Difference Between Shallow And Deep Copy of a Class

BASIS OF COMPARISON  SHALLOW COPYDEEP COPY
Cloned ObjectsCloned Object and original object are not 100% disjoint.Cloned Object and original object are 100% disjoint.
ChangesAny changes made to cloned object will be reflected in original object or vice versa.Any changes made to cloned object will not be reflected in original object or vice versa.
Creating ObjectDefault version of clone method creates the shallow copy of an object.To create the deep copy of an object, you have to override clone method.
PreferenceShallow copy is preferred if an object has only primitive fields.Deep copy is preferred if an object has references to other objects as fields.
Speed  Shallow copy is faster.Deep copy is slow.
Convenience  It is less expensive.It is very expensive.

Conclusion

Deep copy differs from shallow copy in the manner in which the reference type members of the object are copied. While copying the field members of value type in both cases, a bit-by-bit copy of field is performed. When copying fields of reference type, shallow copy involves copying only the reference, whereas in deep copy, a new copy of the referred object is performed.

ncG1vNJzZmiumauupbXFn5yrnZ6YsrR6wqikaJyZm7OmvsSnmp5lkprBuLHEp2SsoJGhubDDjJqlnWWUmrKxecKop7Jln5t6onnCpZisq18%3D