Java Cloning

Clone, Shallow Cloning & Deep Cloning

Clone – Introduction

  • The process of creating exactly duplicate object is called cloning.
  • The main objective of cloning is to maintain backup copy & to preserve state of an object.
  • We can perform cloning by using clone() method of Object class.
  • We can perform cloning only for clonable objects.
  • An object is said to be clonable if and only if the corresponding class implements clonable interface.
  • Cloneable interface is present in java.lang package and it doesn’t contain any methods.
  • It is a marker interface.
  • If we are trying to perform cloning for non-clonable objects then we will get Runtime exception CloneNotSupportedException.
  • The java.lang.Cloneable interface must be implemented by the class whose object clone we want to create. If we don't implement Cloneable interface, clone() method generates CloneNotSupportedException.
  • Need to throws CloneNotSupportedException for main method otherwise will get compile time error.

Sample Program

  • The PDF contains a sample program demonstrating a class implementing Cloneable, creating an object, invoking clone(), changing values, and printing the results.
Sample Program:
The original PDF shows the complete Java sample program as an embedded screenshot on page 2. The program demonstrates implements Cloneable, clone(), and the resulting values.

Shallow Cloning – Bitwise Copy

  • The process of creating bitwise copy of an object is called shallow cloning.
  • If the main object contains primitive variables then exactly duplicate copy will be created in the clone() object.
  • If the main object contains any reference variable then corresponding object won’t be created; just a duplicate reference variable will be created pointing to old contained object.
  • Object class clone() method is meant for shallow cloning.
Visual example from page 3: The PDF illustrates shallow cloning with a diagram showing the cloned object and the shared referenced object.

Deep Cloning

  • In shallow cloning, by using cloned object reference, if we perform any change to the cloned object those changes will be reflected to the main object.
  • To overcome this problem we should go for deep cloning.
  • The process of creating exactly independent copy of an object including contained object is called deep cloning.
  • If the main object contains primitive variables then exactly duplicate copy will be created in the clone() object.
  • If the main object contains any reference variable then corresponding object will be created in the clone() object.
  • Object class clone() method is meant for shallow cloning but we can implement deep clone by overriding the clone() method in our class.
Visual example from page 4: The PDF shows class Dog implements Cloneable and an overridden clone() method, along with a Cat contained object.

Note / Best Fit

  • By using cloned object reference, if we perform any change to the cloned object those changes won’t be reflected to the main object.
  • To overcome this problem we should go for deep cloning.
  • If object contains only primitive variables then shallow cloning is the best choice.
  • If object contains reference variable then deep cloning is the best fit.
Source note: This HTML preserves the explanations and terminology from the supplied PDF. The original PDF contains Java code and cloning diagrams as screenshots; those visual examples are described above rather than reconstructed or silently changed.