Object Cloning in Java Using clone() Method

Object cloning in Java is the process of creating a copy of an object. To clone an object in Java clone() method of the Object class is used.

clone() method

clone() method is defined in the Object class as a protected method.

If you are going to write a clone() method in your class to override the one in Object class then it has to be written as a public method.

Cloning an object in Java using clone() method

One of the requirement for cloning an object is that the class whose objects are to be cloned must implement Cloneable interface. Cloneable interface is a marker interface and defines no members of its own. If you try to call clone() method on a class that does not implement Cloneable interface, a CloneNotSupportedException is thrown.

If you have to create a clone of an object obj using clone() method then you write-

Default implementation of the clone() method creates an object of the same class as the original object. Cloned object’s member variables are initialized to have the same values as the original object’s corresponding member variables.

Examples of Java object cloning

Here is a simple example of cloning an object by directly calling clone() method of the Object class using super.

Output

In the code you can note what all is done for object cloning-

  1. Class whose object is to be cloned implements Cloneable interface.
  2. super.clone() is called to use the default implementation of the clone() method in the Object class.
  3. You can see that the copy of an object has the same state as the original object (same value for the member variables as original object).
  4. Though the state is same but the reference is not same. Cloned object is distinct.

Override clone method in the class
You can also override the clone method in the class though it is not always necessary.

Output

Advantages of object cloning

  1. Object cloning in Java is the easiest way to get a new object with a state. You don’t need to go through the whole process of calling new operator to create an object and assign value to its fields.
  2. Cloned object is a distinct object with its own state. Changing one of the object doesn’t change the state of the other object.

Disadvantages of object cloning

  1. Though using the default clone() method make the process of cloning an object automatic but at the same time you don’t have any control over the whole process. Even the constructor is not called when object is cloned.
  2. Object.clone() method creates a shallow copy. That is fine if the fields in an object are primitive or immutable. In case original object holds reference to another object then that reference is shared between original and cloned object which may be a problem.

Object cloning – Shallow copy

When an object is cloned a bit wise copy happened where each field’s value in the original object is copied to the cloned object’s corresponding field. This way of object cloning in Java is known as Shallow copy. This works fine for primitive values as we have already seen in the examples.

But this process of creating an exact copy may have a problem when object holds reference to another object. In that case that reference is copied as it is in the cloned object and both object share that object reference. That’s when you may need a deep copy.

Java Object cloning – Deep copy

you need to do a deep copy if you don’t want any object references to be copied as it is during the object cloning process.
For creating a deep copy you need to explicitly override the clone() method in your class and call the clone() method for the objects referenced by the original object too.
Creating a Deep copy is more expensive as you need to create new objects for all the referenced objects. It is more complicated too as there may be a whole object tree that you may need to clone.

To know more about Shallow copy and Deep copy in Java object cloning please refer this post- Shallow Copy Vs Deep Copy in Java Object Cloning

Object cloning in Java – Deep copy example

In the example there are two classes One and Two. In Class Two there is a reference to an object of class One. In order to do a deep copy clone() method is overridden in class Two and clone() method for the referenced object (object of class One) is also called explicitly to create a distinct copy of that object too.

Output

As you can see from the output when the str field is modified in class One object referenced by the cloned object that change is not reflected in the original object because of the deep copy.

That’s all for the topic Object Cloning in Java Using clone() Method. If something is missing or you have something to share about the topic please write a comment.


You may also like

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.