Clone method in Java

Clone method in Java

The process of creating an similar copy of an existing object is called ‘cloning’. In java, clone() method of java.lang.Object class is used for cloning process.

There are two types of cloning or clone method in java –

Shallow Cloning –

Shallow copy of an object is the default version of the clone() method. The shallow copy of an object will have exact copy of all the fields of original object. If original object has any references to other object as fields, then only references of those objects are copied into clone object, copy of those object are not created. Hence when the clone object is modified, same modification will also affect the original object or vise-versa.

Deep Cloning –

like shallow copy, Deep copy of an object will have exact copy of all the fields of original object. However, in addition, if original object has any references to other objects as fields, then copy of those objects are also created by calling clone() method on them. That means clone object and original object will be 100% disjoint. They will be 100% independent of each other. Any changes made to clone object will not be reflected in original object or vice-versa.

Steps to clone object of the class –

1). The class whose object to be cloned should implement Cloneable interface. This interface belongs to java.lang package. Cloneable interface indicates that the class object are cloneable. If this interface is not implemented, then that class object cannot be cloned and it will give ClassNotSupportException.

2). clone() method of Object class is used for cloning. Object is the super class for every class. So, let us write our own method in the class and call the clone() method.

Public Object myClone(){
  return.super.clone();
}

3). It is also possible to create object, by overriding the clone() method of Object class. Since clone() method is defined as protected Object clone() in Object class.

Protected Object clone(){
  return.super.clone();
}

Shallow Copy Example

package com.c2s.corejava;

public class Data {
	
	private int id;
	private String name;
	
	public Data(int id, String name) {
		super();
		this.id = id;
		this.name = name;
	}
	
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
}
package com.c2s.corejava;

public class ShallowCopy implements Cloneable{

	private String subject;
	private Data data;
	
	public ShallowCopy(String subject, Data data) {
		super();
		this.subject = subject;
		this.data = data;
	}

	// clone the present class object
	public Object myClone() throws CloneNotSupportedException{
		//Shallow Copy
		return super.clone();
	}
	
	//display method
	
	public void display(){
		System.out.println(data.getId()+" "+data.getName()+" "+subject);
	}

	public String getSubject() {
		return subject;
	}

	public void setSubject(String type) {
		this.subject = type;
	}

	public Data getData() {
		return data;
	}

	public void setData(Data data) {
		this.data = data;
	}
}
package com.c2s.corejava;

public class ShallowCopyDemo {

	public static void main(String[] args) throws CloneNotSupportedException {
		
		Data data = new Data(10, "John");
		
		ShallowCopy object = new ShallowCopy("Physics", data);
		
		//display original object;
		System.out.println("Original Object: ");
		object.display();
		
		//copy of the object
		ShallowCopy cloneObject = (ShallowCopy) object.myClone();
		System.out.println("\nCloned Object: ");
		cloneObject.display();
		
		object.setSubject("Maths");
		object.getData().setId(15);
		object.getData().setName("Paul");
		
		System.out.println("\nOriginal Object after update: ");
		object.display();
		
		System.out.println("\nCloned Object after update: ");
		cloneObject.display();
		
	}

}

Output:

Original Object: 
10 John Physics

Cloned Object: 
10 John Physics

Original Object after update: 
15 Paul Maths

Cloned Object after update: 
15 Paul Physics

 

Deep Copy Example

package com.c2s.corejava;

public class DeepCopy implements Cloneable{

	private String subject;
	private Data data;
	
	public DeepCopy(String subject, Data data) {
		super();
		this.subject = subject;
		this.data = data;
	}

	// clone the present class object
	public Object myClone() throws CloneNotSupportedException{
		//Deep Copy
		Data d = new Data(data.getId(), data.getName());
		DeepCopy dc = new DeepCopy(subject, d);
		return dc;
	}
	
	//display method
	
	public void display(){
		System.out.println(data.getId()+" "+data.getName()+" "+subject);
	}

	public String getSubject() {
		return subject;
	}

	public void setSubject(String type) {
		this.subject = type;
	}

	public Data getData() {
		return data;
	}

	public void setData(Data data) {
		this.data = data;
	}
}
package com.c2s.corejava;

public class DeepCopyDemo {

	public static void main(String[] args) throws CloneNotSupportedException {
		
		Data data = new Data(10, "John");
		
		DeepCopy object = new DeepCopy("Physics", data);
		
		//display original object;
		System.out.println("Original Object: ");
		object.display();
		
		//copy of the object
		DeepCopy cloneObject = (DeepCopy) object.myClone();
		System.out.println("\nCloned Object: ");
		cloneObject.display();
		
		object.setSubject("Maths");
		object.getData().setId(15);
		object.getData().setName("Paul");
		
		System.out.println("\nOriginal Object after update: ");
		object.display();
		
		System.out.println("\nCloned Object after update: ");
		cloneObject.display();
	}
}

Output:

Original Object: 
10 John Physics

Cloned Object: 
10 John Physics

Original Object after update: 
15 Paul Maths

Cloned Object after update: 
10 John Physics

 

Note: ‘Data’ class is common for both examples.

 

Stay tuned for more updates!

Leave a Reply

Your email address will not be published. Required fields are marked *