Cloning

Cloning is actually a biological term. It is the process of obtaining exact copy of a plant or a bird or an animal. This process happens by duplicating the source DNA. The first cloned sheep is called Dolly (white sheep).

Hang on...! Did you say, why biology topic on the technology blog....!

Cloning in Java

Cloning in Java is the process of obtaining the exact copy of an object i.e.; duplicating an existing object. Cloning can be classified into 2 types based on it's implementation. Cloning process has to be initiated by implementing a tagged or marked interface java.lang.Cloneable.

You would have probably observed that the super class java.lang.Object has a method implemented called as clone(). clone() method will provide the exact copy of the source object i.e.; the object which invokes the clone() method. Since this method returns java.lang.Object, one has to type caste it.

Shallow cloning

If a new object is obtained just by copying the instance variable reference of the source object, we can call this implementation as Shallow cloning. Any modifications to the original object will also effect the cloned object. This is the default implementation that Javasoft has defined (as said above, cloning with clone() method)

Let's consider an example, a Java object, obj has an ArrayList instance variable. Cloning this object will exactly create another object with the same ArrayList as the reference. i.e.; it just copies the memory reference.

Employee.java

/**
* This class demonstrates the shallow cloning.
* @author SANTHOSH REDDY MANDADI
* @version 1.0
* @since 27 August 2012
*/

public class Employee implements Cloneable
{
int id;
String name;

public Employee(int id, String name)
{
this.id = id;
this.name = name;
}

public void setId(int id)
{
this.id = id;
}

public int getId()
{
return id;
}

public void setName()
{
this.name = name;
}

public String getName()
{
return name;
}

public static void main(String args[]) throws CloneNotSupportedException
{
Employee e = new Employee(1, "Santhosh Reddy");
System.out.println("*****Source object*****");
System.out.println("Employee id: "+e.getId()+" & Employee name: "+e.getName());
Employee e1 = (Employee)e.clone();
System.out.println("*****Cloned object*****");
System.out.println("Employee id: "+e1.getId()+" & Employee name: "+e1.getName());
}
}
Output

>java Employee
*****Source object*****
Employee id: 1 & Employee name: Santhosh Reddy
*****Cloned object*****
Employee id: 1 & Employee name: Santhosh Reddy
Explanation

Please note that the above class "Employee" is implementing the java.lang.Cloneable interface. This means to JVM that any object of this class can be cloned. If you want to give a try, you can remove this implementation and the program will compile but it will raise java.lang.CloneNotSupportedException. So, implementing Cloneable interface is must in order to implement Shallow cloning.

Deep cloning

In Deep cloning, all the objects which are part of source object are cloned recursively. i.e.; modifications to the original object will not effect the cloned object. This implementation has to be done explicitly by us.