In Java, cloning refers to the process of creating a duplicate copy of an object.
This can be achieved using the "Cloneable" interface and the "clone()" method.
However, there are different types of cloning, primarily "shallow cloning" and "deep cloning".
Shallow cloning creates a new object and copies all of the fields of the original object to the new object.
However, if the fields of the original object are references to other objects, only the references are copied, not the objects themselves. Thus, the copied object shares references to the same objects as the original object.
The default implementation of "clone()" method provided by the Object class performs shallow cloning.
Shallow cloning can be insufficient if the object being cloned contains mutable objects. Changes made to the mutable objects within the cloned object will reflect in both the original and cloned objects.
class Employee implements Cloneable { private String name; private Department department; public Employee (String name, Department department) { this.name = name; this.department = department; } @Override public Object clone () throws CloneNotSupportedException { return super.clone (); } // Getters and setters public Department getDepartment () { return department; } public void setDepartment (Department department) { this.department = department; } } class Department { private String deptId; public Department (String deptId) { this.deptId = deptId; } // Getters and setters public String getDeptId () { return deptId; } public void setDeptId (String deptId) { this.deptId = deptId; } } public class Main { public static void main (String[]args) { Department department = new Department ("D112"); Employee original = new Employee ("Alice", department); try { Employee cloned = (Employee) original.clone (); cloned.getDepartment ().setDeptId ("D123"); System.out.println ("Original: " + original.getDepartment ().getDeptId ()); // Output: D123 System.out.println ("Cloned: " + cloned.getDepartment ().getDeptId ()); // Output: D123 } catch (CloneNotSupportedException e) { e.printStackTrace (); } } }
class Employee implements Cloneable { private String name; private Department department; public Employee (String name, Department department) { this.name = name; this.department = department; } @Override public Object clone () throws CloneNotSupportedException { Employee cloned = (Employee) super.clone(); cloned.department = (Department) department.clone(); // Deep copy for Address return cloned; } // Getters and setters public Department getDepartment () { return department; } public void setDepartment (Department department) { this.department = department; } } class Department implements Cloneable { private String deptId; public Department (String deptId) { this.deptId = deptId; } @Override public Object clone() throws CloneNotSupportedException { return super.clone(); } // Getters and setters public String getDeptId () { return deptId; } public void setDeptId (String deptId) { this.deptId = deptId; } } public class Main { public static void main (String[]args) { Department department = new Department ("D112"); Employee original = new Employee ("Alice", department); try { Employee cloned = (Employee) original.clone (); cloned.getDepartment ().setDeptId ("D123"); System.out.println ("Original: " + original.getDepartment ().getDeptId ()); // Output: D123 System.out.println ("Cloned: " + cloned.getDepartment ().getDeptId ()); // Output: D123 } catch (CloneNotSupportedException e) { e.printStackTrace (); } } }
Deep cloning creates a new object and recursively copies all fields of the original object as well as all the objects referenced by those fields, and so on, until all objects in the object graph have been copied.
Achieving deep cloning typically requires custom implementation, as the default "clone()" method only performs shallow cloning.
Deep cloning ensures that changes made to the cloned object or any of its nested objects do not affect the original object or its nested objects.
we ensure "deep cloning" by explicitly cloning the "Department" object within the "Employee" clone method.
Now, modifying the "deptId" in the cloned object doesn't affect the original object, demonstrating the independence achieved through deep cloning.