Object Cloning in Java

2
0

Introduction

Object cloning in Java is the process of creating a copy of an existing object. The cloned object contains the same field values as the original object but occupies a different memory location.

Java supports cloning through:

  • The Cloneable interface

  • The clone() method of the Object class

Why Is Object Cloning Used?

Object cloning is useful when:

  • An existing object needs to be duplicated

  • The current state of an object must be preserved

  • Reinitializing an object would require additional work

  • A separate copy is required before making changes

How Object Cloning Works

To clone an object in Java:

  1. The class implements the Cloneable interface.

  2. The class overrides the clone() method.

  3. The super.clone() method creates the copy.

class ClassName implements Cloneable {
@Override
public ClassName clone() throws CloneNotSupportedException {
return (ClassName) super.clone();
}
}

Cloneable is a marker interface, meaning it does not contain methods. If a class does not implement it, calling Object.clone() results in a CloneNotSupportedException.

Object Cloning in Java

Object Cloning in Java

Types of Object Cloning

Java object cloning is mainly divided into:

  1. Shallow cloning

  2. Deep cloning

Shallow Cloning in Java

Shallow cloning creates a new outer object but copies references to nested objects.

This means:

  • Primitive values are copied.

  • The outer cloned object is separate.

  • Referenced mutable objects remain shared.

  • Changing a shared nested object affects both copies.

The default Object.clone() method performs a shallow copy.

Shallow Cloning Example

// Address is a nested object used by Person
class Address {
// Store the city name
String city;
// Initialize the city
Address(String city) {
this.city = city;
}
}
// Person supports cloning
class Person implements Cloneable {
// Store the person's name
String name;
// Store a reference to an Address object
Address address;
// Initialize the Person object
Person(String name, Address address) {
this.name = name;
this.address = address;
}
// Create a shallow copy using Object.clone()
@Override
public Person clone() throws CloneNotSupportedException {
return (Person) super.clone();
}
}
public class Main {
public static void main(String[] args)
throws CloneNotSupportedException {
// Create the original nested object
Address address = new Address("Mumbai");
// Create the original Person
Person original = new Person("Rahul", address);
// Create a shallow clone
Person cloned = original.clone();
// Change the nested Address through the cloned object
cloned.address.city = "New Delhi";
// Both objects refer to the same Address
System.out.println(original.name + " lives in " + original.address.city); // Output :- Rahul lives in New Delhi
System.out.println(cloned.name + " lives in " + cloned.address.city); // Output :- Rahul lives in New Delhi
}
}

The original and cloned objects are different, but their address fields refer to the same Address object.

Shallow Cloning in Java

Shallow Cloning in Java

Deep Cloning in Java

Deep cloning creates a separate copy of the outer object and its nested mutable objects.

This means:

  • Primitive values are copied.

  • Nested objects are also copied.

  • The original and cloned objects do not share mutable nested data.

  • Changes to the clone do not affect the original.

Java does not perform deep cloning automatically through super.clone(). Nested objects must also be copied manually.

Deep Cloning Example

// Address also supports cloning
class Address implements Cloneable {
// Store the city name
String city;
// Initialize the city
Address(String city) {
this.city = city;
}
// Create a separate Address copy
@Override
public Address clone() throws CloneNotSupportedException {
return (Address) super.clone();
}
}
// Person supports deep cloning
class Person implements Cloneable {
// Store the person's name
String name;
// Store the nested Address object
Address address;
// Initialize the Person object
Person(String name, Address address) {
this.name = name;
this.address = address;
}
// Create a deep copy of Person
@Override
public Person clone() throws CloneNotSupportedException {
// First create a shallow copy of Person
Person cloned = (Person) super.clone();
// Then create a separate copy of the nested Address
cloned.address = address.clone();
// Return the fully independent copy
return cloned;
}
}
public class Main {
public static void main(String[] args)
throws CloneNotSupportedException {
// Create the original Address and Person
Address address = new Address("Mumbai");
Person original = new Person("Rahul", address);
// Create a deep clone
Person cloned = original.clone();
// Change only the cloned person's Address
cloned.address.city = "New Delhi";
// The original Address remains unchanged
System.out.println(original.name + " lives in " + original.address.city); // Output :- Rahul lives in Mumbai
System.out.println(cloned.name + " lives in " + cloned.address.city); // Output :- Rahul lives in New Delhi
}
}

Here, the Address object is also cloned. Therefore, changing the cloned person’s city does not affect the original person.

Deep Cloning in Java

Deep Cloning in Java

Shallow Cloning vs Deep Cloning

Shallow Cloning

Deep Cloning

Creates a new outer object

Creates a new outer object

Copies primitive values

Copies primitive values

Shares nested object references

Copies nested objects

Nested changes may affect both copies

Nested changes remain independent

Easier and faster to implement

Requires additional cloning logic

Suitable when nested objects are immutable or sharing is acceptable

Suitable when complete independence is required

Summary

Object cloning in Java creates a copy of an existing object using the Cloneable interface and clone() method.

Shallow cloning creates a new outer object but shares nested object references. Deep cloning copies both the outer object and its nested mutable objects, producing a more independent copy.

CS Core

Read Similar Blogs

Comments0