Object cloning in Java is the process of creating an exact copy of an existing object. The clone() method, defined in the Object class, is used to perform cloning and create a new object with the same state as the original object.
- Reduces manual effort compared to copy constructors.
- Throws CloneNotSupportedException if cloning is not supported.
- Helps duplicate object state efficiently.
Syntax
class MyClass implements Cloneable {
@Override
public Object clone() throws CloneNotSupportedException {
return super.clone();
}
}
Example: Program to demonstrate object cloning using the clone() method
class GFG implements Cloneable {
int x, y;
// Constructor to initialize
// object fields
GFG() {
x = 10;
y = 20;
}
// Overriding the clone() method
@Override
public Object clone() throws CloneNotSupportedException {
// Returning a clone of the current object
return super.clone();
}
}
public class Main {
public static void main(String[] args)
throws CloneNotSupportedException {
GFG o1 = new GFG();
// Cloning obj1 into obj2
GFG o2 = (GFG) o1.clone();
System.out.println("o1: " + o1.x + " " + o1.y);
System.out.println("o2: " + o2.x + " " + o2.y);
}
}
Output
o1: 10 20 o2: 10 20
Explanation: The object o1 is cloned using the clone() method, creating a new object o2 with the same field values. Although both objects contain identical data, they occupy different memory locations.
Example: Program to demonstrate object cloning (shallow copy)
class GFG implements Cloneable {
String n;
int a;
// Constructor to initialize GFG object
public GFG(String n, int a) {
this.n = n;
this.a = a;
}
// Overriding clone() method
// to make it public
@Override
public Object clone() throws CloneNotSupportedException {
return super.clone();
}
@Override
public String toString() {
return "GFG{name:'" + n + "', age:" + a + '}';
}
}
public class Main {
public static void main(String[] args) {
try {
// Create a Person object
GFG p1 = new GFG("Ram", 24);
// Clone the Person object
GFG p2 = (GFG) p1.clone();
// Print both objects
System.out.println("Original: " + p1);
System.out.println("Cloned: " + p2);
} catch (CloneNotSupportedException e) {
e.printStackTrace();
}
}
}
Output
Original: GFG{name:'Ram', age:24}
Cloned: GFG{name:'Ram', age:24}
Explanation: In the above example, the clone() method is overridden in the GFG class to perform a shallow copy of the object. This creates a new object "p2" with the same values as the original object "p1", but changes to immutable fields like String do not affect each other.
Example: Program to demonstrate deep copy using clone()
// An object reference of this
// class is contained by Test2
class Test {
int x, y;
}
// Contains a reference of Test and
// implements clone with deep copy.
class Test2 implements Cloneable {
int a, b;
Test c = new Test();
public Object clone() throws CloneNotSupportedException
{
// Assign the shallow copy to
// new reference variable t
Test2 t = (Test2)super.clone();
// Creating a deep copy for c
t.c = new Test();
t.c.x = c.x;
t.c.y = c.y;
// Create a new object for the field c
// and assign it to shallow copy obtained,
// to make it a deep copy
return t;
}
}
public class Main {
public static void main(String args[])
throws CloneNotSupportedException
{
Test2 t1 = new Test2();
t1.a = 10;
t1.b = 20;
t1.c.x = 30;
t1.c.y = 40;
Test2 t3 = (Test2)t1.clone();
t3.a = 100;
// Change in primitive type of t2 will
// not be reflected in t1 field
t3.c.x = 300;
// Change in object type field of t2 will
// not be reflected in t1(deep copy)
System.out.println(t1.a + " " + t1.b + " " + t1.c.x
+ " " + t1.c.y);
System.out.println(t3.a + " " + t3.b + " " + t3.c.x
+ " " + t3.c.y);
}
}
Output
10 20 30 40 100 20 300 40
Explanation: In the above example, we can see that a new object for the Test class has been assigned to copy an object that will be returned to the clone method. Due to this, t3 will obtain a deep copy of the object t1. So any changes made in the ‘c’ object fields by t3, will not be reflected in t1.
Shallow Copy Vs Deep Copy
| Feature | Shallow Copy | Deep Copy |
|---|---|---|
| Object Creation | New object created | New object created |
| Referenced Objects | Shared between copies | Independently copied |
| Memory Usage | Less | More |
| Performance | Faster | Slower |
| Data Independence | Lower | Higher |
Advantages of the clone() Method
- Creates object copies quickly and efficiently.
- Reduces the need for manually copying fields.
- Preserves the state of the original object.
- Useful when duplicate objects are required.
- Simplifies object duplication in large classes.
Note: We can use the Assignment Operator to create a copy of the reference variable. This creates a shallow copy, means both variables will refer to the same object in memory. This is not the same as cloning, where a new object is created.