Understanding Object Cloning in Java with Examples


In this article we discuss the Cloneable interface that indicates that a class has provided a safe clone() method. To understand what cloning means recall what happens when you make a copy of a variable holding an object reference. The original and the copy are references to the same object. This means the original and the copy are mutually dependent, i.e., a change in one causes a change in the other as well.

If we would like a copy to be a new object that begins its life being identical to the original but whose state can change over time we must use the clone() method.

The clone() method is declared protected in the Object class, so our code can’t simply call obj.clone(). Now we might ask, but aren’t protected methods accessible from any subclass and isn’t every class a subclass of Object. Fortunately the rules for protected access are much more subtle. A subclass can call protected clone() method to clone its own objects. We must redefine clone to be public to be accessed by any methods.



Even though the default implementation of clone is adequate, you still need to implement the Clonable interface, redefine clone() method to be public, and call public.

Example:

filter_none

edit
close

play_arrow

link
brightness_4
code

class Student implements Cloneable {
  
    // raise visibility level to public
    // and change the return type
    public Student clone()
        throws CloneNotSupportedException
    {
        return (Student)super.clone();
    }
    .
        .
        .
}

chevron_right


The clone() method that you just saw adds no functionality to the copy provided by Object.clone. It merely makes the method public and changes its return type. To make a deeper copy, we must clone the mutable instance fields.

This is the modified version:

filter_none

edit
close

play_arrow

link
brightness_4
code

class Student implements Cloneable {
  
    // other components
  
    public Student clone()
        throws CloneNotSupportedException
    {
  
        // call Object.clone()
        Student obj = (Student)super.clone();
  
        // clone mutable fields
        obj.birthDay = (Date)birthDay.clone();
    }
}

chevron_right


The clone() method of Object will try to throw a ClassNotSupportedException whenever clone is invoked on a class that does not implement the Cloneable interface.

Example:

filter_none

edit
close

play_arrow

link
brightness_4
code

import java.util.Date;
import java.util.GregorianCalendar;
  
public class Employee implements Cloneable {
  
    private String name;
    private double salary;
    private Date hireDay;
  
    public Employee(String name, double salary)
    {
        this.name = name;
        this.salary = salary;
        hireDay = new Date();
    }
  
    public Employee clone()
        throws CloneNotSupportedException
    {
  
        // call Object.clone()
        Employee obj = (Employee)super.clone();
  
        // clone mutable fields
        obj.hireDay = (Date)hireDay.clone();
  
        return obj;
    }
  
    // Set the hireDay to a given date
    public void setHireDay(int year, int month, int day)
    {
  
        Date newHireDay
            = new GregorianCalendar(year,
                                    month - 1,
                                    day)
                  .getTime();
  
        // instance field mutation
        hireDay.setTime(newHireDay.getTime());
    }
  
    public void raiseSalary(double byPercent)
    {
  
        double raise = salary * byPercent / 100;
        salary += raise;
    }
  
    public String toString()
    {
        return ("Employee[name=" + name
                + ", salary=" + salary
                + ", hireDay=" + hireDay
                + "]");
    }
  
    // main
    public static void main(String[] args)
    {
  
        try {
  
            Employee original
                = new Employee("ABC X. YZ", 50000);
  
            original.setHireDay(2000, 1, 1);
            Employee copy = original.clone();
  
            copy.raiseSalary(10);
            copy.setHireDay(2002, 12, 31);
  
            System.out.println("original= " + original);
            System.out.println("copy= " + copy);
        }
  
        catch (CloneNotSupportedException e) {
            e.printStackTrace();
        }
    }
}

chevron_right


Output:

original= Employee[name=ABC X. YZ, salary=50000.0, hireDay=Sat Jan 01 00:00:00 UTC 2000]
copy= Employee[name=ABC X. YZ, salary=55000.0, hireDay=Tue Dec 31 00:00:00 UTC 2002]

Advantages of Cloning:

  • In Java, the ‘=’ (assignment) operator cannot be used for cloning as it simply creates a copy of reference variables. To overcome such discrepancy the clone() method of Object class can be used over assignment operator.
  • The clone() method is protected method of class Object which means that only Employee class can clone Employee objects. This means no class other than Employee can clone Employee objects since it does not know Employee class’ attributes.

Application of Cloning in Java:

  • It allows field-by-field copying of objects which comes handy when dealing with objects of similar characteristics.
  • The default clone() method can be patched up by calling clone on mutable subobjects.


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.