What does cloning mean?

Cloning is the process of creating a copy of something. By default, the following is the behaviour when a clone of an object is created in java.

— If say the class has only primitive data types, then a completely new object copy is returned

— If the class contains any members of class type (not primitive), then cloning returns a reference to the members of the original class. What this means is that both the cloned copy and the original object have member references that point to the same object.

Point (2) might seem to be a bit daunting and convoluted. I will try to explain it with an example.

Say, we have the following classes :

package com.java.demo;public class Department {private int id;private String name;public Department(int id, String name) {this.id = id;this.name = name;}public int getId() {return this.id;}public String getName() {return this.name;}public void setId(int id) {this.id = id;}public void setName(String name) {this.name = name;}}

Class Employee :

package com.java.demo;public class Employee implements Cloneable {private int id;private String name;private Department dept;public Employee( int id,  String name, Department dept) {this.id = id;this.name = name;this.dept = dept;}public int getId() {return this.id;}public String getName() {return this.name;}public Department getDepartment() {return this.dept;}public void setId(int id) {this.id = id;}public void setName(String name) {this.name = name;}public void setDepartment(Department dept) {this.dept = dept;}@Overrideprotected Object clone() throws CloneNotSupportedException {return super.clone();}}

To test the cloning mechanism :

package com.java.demo;public class TestCloning {public static void main(String args[]) throws CloneNotSupportedException {Department dept = new Department(1, "Sales");Employee emp = new Employee(1, "Peter", dept);Employee cloned = (Employee) emp.clone();dept.setName("Marketing");System.out.println(emp.getDepartment().getName());System.out.println(cloned.getDepartment().getName());}}

The output of the java application is :

MarketingMarketing

Thus, what happens is that when a cloned object of Employee — ‘cloned’ is created, its reference points to the same object dept created for and used by Employee. Hence, when we change the dept name, it affect both the Employee object as well as its cloned object. This is known as shallow copy. The way to mend this situation would be using something known as a deep copy.

For a deep copy, the following change needs to be made to the Employee class :

@Overrideprotected Object clone() throws CloneNotSupportedException {Employee cloned = (Employee)super.clone();cloned.setDepartment((Department)cloned.getDepartment().clone());return cloned;}

We also need to override the clone method and implement the Cloneable interface for the Department class.

Reference : https://howtodoinjava.com/java/cloning/a-guide-to-object-cloning-in-java/

I’m a software engineer with a strong interest in algorithms. I love solving puzzles and love the aha moment of arriving at a solution after hours of struggle.