View RSS Feed

Object Copy

Object clone in Java

  1. Shallow cloning Vs Deep cloning

    by , 01-08-2012 at 12:36 PM
    By default an object’s clone() yields a shallow copy. You must edit your class to get a deep copy of the cloned object.

    • Shallow copy: As shown in figure, when an object is shallow copied, it does not copy its internal objects. Shallow cloning is supported by default in Java. Implement the interface java.lang.Cloneable to implement shallow cloning.
    • Deep copy: In deep cloning, all the internal objects of the class are also copied as shown in figure below. Serialization supports ...