Home‎ > ‎Applied Core Java‎ > ‎

Clonable Interface

Introduction on Clonable Interface

When an object is copied to the other using assignment operator, only reference or meta data of the object is copied. So change in one object reflects in other. Java use clone() method of Object class to copy content of one object to the other , which is also known as “Shallow Copy”.

clone() is a method in the Java programming language for object duplication and found in clonable interface.. The Cloneable interface defines no members. It is used to indicate that a class allows a bitwise copy of an object (that is, a clone) to be made. If you try to call clone( ) on a class that does not implement Cloneable, a CloneNotSupportedException is thrown . The clone() method generates a duplicate copy of the object on which it is called. Only classes that implement the Cloneable interface can be cloned.

Types of cloning :

  •  Shallow cloning : 

In Shallow cloning, the object is copied without its contained (sub) objects. Shallow clone only copies the top level structure of the object not the lower levels.   It only copies the references of the sub objects.

  •  Deep Cloning :

In deep cloning, 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 .

Below is the diagramatic presentataion of Shallow and Deep Cloning 

1.  Two classes, Customer and Account.  Customer contains an account object.

    // Account class with clonable interface

public class Account  implements Cloneable {

int accountId ;

double balance ;


// Customer class having account object

public class Customer implements Cloneable {

int custId ;

String name;

Account acc ;


 2.Copying only the references :

public static void main (String[] args) throws Exception{
Customer c1= new Customer();
Customer c2= c1;  // copy only the reference of object

3. Shallow Cloning :

we have two distinct objects: c1 and c2. If using equals() method on name attribute, it tells us that the "names" are equivalent, but == tells us that they aren't equal - that is, they  are two distinct objects. This is what  referred  as a "Shallow Copy". It creates copy for the customer class but do not create the copy for account class . That is, both object share  the same reference of Account class           

public static void main (String[] args) throws Exception{
Customer c1= new Customer();
Customer c3= (Customer)c1.clone();//calls default Object.clone()method.And perform Shallow Cloning.

4. Deep Cloning :

We can instead have not only distinct objects (i.e. c1 and c2) but  also having separate copies of all of its instance variables
Deep cloning can be achieved by overriding Clone() method in customer class ,
          // Account Class implements clonable interface

public class Account implements Cloneable {

public Object clone() throws CloneNotSupportedException
return super.clone();
 // Customer class override clone method
public class Customer implements Cloneable {
public Object clone() throws CloneNotSupportedException
Customer obj= (Customer)super.clone();
return obj;
public static void main (String[] args) throws Exception{
Customer c1= new Customer();
Customer c3= (Customer)c1.clone();// calls the override clone method.

Classes that want to allow cloning must implement the marker interface Cloneable. Since the default implementation of Object.clone only performs a shallow copy, classes must also override clone to provide a custom implementation when a deep copy is desired.

Conclusion :

The side effects caused by cloning are sometimes difficult to see at first. It is easy to think that a class is safe for cloning when it actually is not. Disadvantages of Deep Cloning are :
  • Serialization is more expensive than using object.clone().
  • Not all objects are serializable.
  • Serialization is not simple to implement for deep cloned object.
  • There is no need to deal with static fields when performing a cloning
    operation. There is only one memory location reserved for each static
    field per class, per application domain.


Q.1 why objects are not cloneable by default.

A . It would make no sense to assume that all objects can be sensibly  duplicated with a shallow copy. Likewise, it makes no sense to assume that a  deep copy would be necessary .

<<Previous | Next>>

Namrata Sisodiya,
Mar 29, 2011, 5:54 AM