Deep diving into Cloning

Before we proceed with the cloning concept let’s refresh our basics with the object creation concept. When the objects are created using the new operator the objects gets the memory allocation in the Heap.

Object Creation in Heap

Object Creation in Heap

In Java ideally objects are modified through reference variable only i.e. only the memory address of the object is copied and hence any changes in the original object will be reflected in the new variable.

Glass objGlass1=new Glass();

Glass objGlass2=objGlass1;

Here, in this case any changes you make to object objGlass1 will reflect in object objGlass2 and vice versa. That means ‘objGlass1== objGlass2‘ will return true both the reference variables objGlass1 and objGlass2 are referring to the same object. However if you are intending to copy the object unlike just copying the reference of the object then you will need cloning.

What is cloning?

Cloning is a process of copying an object i.e. creating a new instance by copying itself. Cloning in Java can be done by use of clone() method of the object.

Cloning creates and returns a copy of the object, with the same class and with all the fields having the same values.

Glass objGlass1 = new Glass();

Glass objGlass2 = (Glass) objGlass.clone();

Let’s see the below analysis after cloning:

  • objGlass1!=objGlass2  returns TRUE which means that objGlass1 and objGlass2  are referring to two different memory locations i.e. two different objects.
  • objGlass1.getClass()==objGlass2 .getClass() returns TRUE which means the cloned object and the original object should be of the same type.
  • objGlass1.equals(objGlass2) returns TRUE which means the cloned object data should be equal to the original one(However it can be changed any time after cloning).

Shallow Cloning vs Deep Cloning

Java supports two types of Cloning – Shallow Cloning and Deep Cloning.

In case of Shallow cloning a new object is created that has an exact copy of the values in the original object. The clone() method  of the Object provides the Shallow cloning. In this cloning mechanism the object is copied without its contained objects.

Shallow clone only copies the top level structure of the object not the lower levels.

Structure:

Shallow Cloning Structure

Shallow Cloning Structure

In the above diagram the OriginalObject1 has Field1 and a object contained called ReferenceObject1. Now during Shallow cloning of OriginalObject1 the ClonedObject2 is created with Field2 having the copied value from Field1 and it still points to the ReferenceObject1. The reason behind this is the Field1 is of primitive type so its values are copied into a Field2. However since ReferenceObject1 is an Object type the ClonedObject2 points to the same ReferenceObject1.

Any changes made to ReferenceObject1 will be visible ClonedObject2.

Shallow Cloning Structure

Shallow Cloning Structure

In case of Deep Cloning all the fields are copied. In this case even the objects referenced are copied in the cloned object along with the fields.

Deep Cloning Structure

Deep Cloning Structure

As mentioned in the above figure the OriginalObject1 has a primitive type Field1 and a ReferenceObject1. Now when we do a deep cloning of the OriginalObject1 then the ClonedObject2 is created along with Field2 is having the copied values from Field1 and ReferenceObject2 containing the copied values of ReferenceObject1.

Deep Cloning Structure

Deep Cloning Structure

Example of Shallow Cloning:

Shallow Cloning Example

Shallow Cloning Example

Shallow Cloning Example

In the above example we have an original object Employee which has reference to Department class and a field EmployeeName. At the first instance let’s assume that the values of the EmployeeName= “Chris” and DepartmentName =”Sales”. When we clone the object Employee through Shallow Cloning then a ClonedEmployee object is created which has a duplicate field cloned EmployeeName and a Department. However we need to note that there is no duplicate Department object created. The cloned Employee object refers the same memory address of the referenced class Department.

So now when we change the original object values of EmployeeName to “Peter” and DepartmentName to “Finance” then the cloned EmployeeName field does not change. It still contains the old value (as per the above section of the diagram). However we must notice that the cloned DepartmentName has been modified now to “Finance” to reflect the change. This is because the cloned Employee refers to the same memory address as of the original object. So any change made to the original object reference is also visible to the cloned object referencing the original object. It doesn’t get duplicated like the fields.

Code Example for Shallow Cloning

Department.java(ReferenceObject)

public class Department {
	private String deptName;

	public Department(String str) {
		deptName = str;
	}

	public String getDeptName() {
		return deptName;
	}

	public void setDeptName(String deptName) {
		this.deptName = deptName;
	}
}

Next Page

Trackbacks/Pingbacks

  1. Creating another dolly with Prototype | IdioTechie - October 31, 2012

    […] The above example uses Shallow cloning method. However we can implement through deep cloning as well. A detailed explanation on this topic can be found in our article: Deep diving into Cloning. […]

Leave a Reply