Shallow cloning:

  •  Shallow cloning is a technique used to create a new object that is a copy of an existing object.
  • In shallow cloning, only the fields of the object being cloned are copied to the new object.
  • If the object contains references to other objects, the references themselves are copied, not the actual referred objects.
  • As a result, the cloned object and the original object share references to the same objects.

Here are some key points to understand about shallow cloning:

  • Default Cloning Mechanism: Shallow cloning can be achieved using the default cloning mechanism provided by Java.
  • To enable cloning, the class needs to implement the Cloneable interface and override the clone() method inherited from the Object class.
  • Copying Field Values: During shallow cloning, the clone() method typically calls the superclass's clone() method to create a new instance and then copies the field values of the original object to the new instance.
  • This process is performed by the default clone() method in the Object class.

  • Shared References: Shallow cloning creates a new object that shares references to the same objects as the original object.
  • If the object being cloned contains references to other objects, both the original and cloned objects refer to the same referenced objects.
  • Any modifications made to the shared referenced objects will be visible from both the original and cloned objects.

  • Limited Independence: As shallow cloning creates a new object with shared references, it provides limited independence between the original and cloned objects.
  • If modifications are made to the referenced objects, both the original and cloned objects will see the changes.

  • Performance Advantage: Shallow cloning can be faster and more memory-efficient compared to deep cloning because it only copies the fields of the object being cloned and does not traverse and copy the entire object graph.

  • Risk of Inconsistent State: Since the cloned object and the original object share references to the same objects, changes made to the referenced objects can potentially lead to an inconsistent state if not handled carefully.
  • It's crucial to understand the implications of shared references and manage the state of referenced objects accordingly.

Here's an example to illustrate shallow cloning:


class MyClass implements Cloneable
private int value;
private MyObject myObject;
public MyClass(int value, MyObject myObject)
this.value = value;
this.myObject = myObject; 
 } 
public void setValue(int value)
this.value = value; 
 }
public int getValue()
return value; 
 }
public MyObject getMyObject() {
return myObject; 
 }
@Override
public Object clone() throws CloneNotSupportedException { 
return super.clone(); 
 } } 
public class ShallowCloningExample
public static void main(String[] args)
MyObject originalObject = new MyObject(10);
MyClass original = new MyClass(1, originalObject); 
try { MyClass clone = (MyClass) original.clone(); 
 System.out.println("Original Value: " + original.getValue()); System.out.println("Clone Value: " + clone.getValue());
 clone.setValue(2); clone.getMyObject().setValue(20);
 System.out.println("Original Value after modifying clone: " + original.getValue());
 System.out.println("Clone Value after modifying clone: " + clone.getValue());
 System.out.println("Original Object value after modifying clone: " + original.getMyObject().getValue());
 System.out.println("Clone Object value after modifying clone: " + clone.getMyObject().getValue()); 
 }
catch (CloneNotSupportedException e) {
 e.printStackTrace(); }
 } }

  • In the example above, the MyClass class contains an int field named value and a reference to MyObject.
  • The clone() method performs shallow cloning, which means the referenced MyObject is not deep cloned.
  • When modifying the cloned object's value or the referenced MyObject's value, the changes are visible in both the original and cloned objects.
  • This is because they share the same referenced MyObject.