Object Cloning in Java

The object cloning is a way to create an exact copy of the content of an object.

Java uses clone() method to Object class to clone an object, which is also known as Shallow copy.

Reference Copy Shallow Copy

Accoount a1 = new Account(10);

Account a2 = a1;

Account a1 = new Account(10);

Account a2 = a1.clone();

A2.balance = 20;

Java.lang.the cloneable interface must be implemented by the class whose object clone you want to create otherwise clone() method will raise cloneNotSupportedException.

Interface Cloneable is a marker interface and does not contain any method.

Example:

Look at the below program that shows how to create a clone of the object.

Here you can see that the value of the current object and the value of the cloned object is different.

class BankAccount implements Cloneable {

	public double balance = 0;

	public BankAccount(double b) {
		balance = b;
	}

	public Object clone() throws CloneNotSupportedException {
		return super.clone();
	}

}

public class TestBankAccount {

	public static void main(String[] args) throws CloneNotSupportedException {
		BankAccount a1 = new BankAccount(10);
		BankAccount a2 = (BankAccount) a1.clone();
		a2.balance = 20;
		System.out.println(a1.balance);
		System.out.println(a2.balance);
	}
}
Example Output

Types of Object Cloning in Java

  • Shallow cloning
  • Deep Cloning

Shallow cloning

In shallow cloning, the object is copied without its contained (subObjects) objects. Shallow clone only copies the top level structure of the object, not the lower levels. It only copies the references to the subjects. It is a default cloning and does not need to write any custom code.

Deep Cloning

In deep cloning, the complete duplicate copy of the original object is created. It copies not only the primitive values of the original objects but also copies all its sub-objects as well.

Example:

Here Customer class is implementing deep cloning, and clone the BankAccount object.

package test;

class BankAccount implements Cloneable {

	public double balance = 0;

	public BankAccount(double b) {
		balance = b;
	}

	public Object clone() throws CloneNotSupportedException {
		return super.clone();
	}
}

class Address implements Cloneable {
	public String city;

	public Address(String city) {
		this.city = city;
	}

	public Object clone() throws CloneNotSupportedException {
		return super.clone();
	}
}

class Customer implements Cloneable {

	public String name = null;
	public Address address;
	public BankAccount account = null;

	public Customer(String name) {
		super();
		this.name = name;
		account = new BankAccount(10);
		address = new Address("Mumbai");
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see java.lang.Object#clone()
	 */
	@Override
	protected Object clone() throws CloneNotSupportedException {
		Customer c = (Customer) super.clone();
		c.account = (BankAccount) account.clone();
		return c;
	}
}

public class TestBankAccount {

	public static void main(String[] args) throws CloneNotSupportedException {

		Customer c1 = new Customer("Kapil");

		// clone the customer object and change the value
		Customer c2 = (Customer) c1.clone();
		c2.name = "Ram";
		c2.account.balance = 50;

		System.out.println("Original Object");
		System.out.println("name : " + c1.name);
		System.out.println("balance : " + c1.account.balance);
		System.out.println("**********");
		System.out.println("Cloned Object");
		System.out.println("name : " + c2.name);
		System.out.println("balance : " + c2.account.balance);

	}

}
Example Output

Share