Cloning Duplicate Object Marker Interface


We have seen earlier, shallow copying is easier and deep copying takes more time, especially, when properties (variables) are many. Here comes another way of copying, cloning. In cloning, object to object is assigned, but at the same time encapsulation is maintained. Both objects occupy two different locations; that is, cloning creates a duplicate object which is no way connected with the original one.

Cloning – interface Cloneable and clone() method

In cloning, the interface Cloneable and method clone() of Object class are used. To clone an object, the class should implement Cloneable interface; else, the JVM throws a checked exception, CloneNotSupportedException.

Example on Cloning Duplicate Object Marker Interface
public class Worker implements Cloneable
{
  int salary;
  public static void main(String args[])
  {
    Worker work1 = new Worker();
    work1.salary = 3000;
      
    try
    {
      Worker work2 = (Worker) work1.clone();

      System.out.println("After cloning, work2 salary: " + work2.salary);    // 3000
      work1.salary = 4000;                         // change work1 salary
      System.out.println("After changing work1 salary, work2 salary: "+ work2.salary);    // 3000

      work2.salary = 5000;                         // now change work2 salary
      System.out.println("After changing work2 salary, work1 salary: "+ work1.salary);    // 4000
                                                         // let us see the hash codes
      System.out.println("Hash code of work1: " + work1.hashCode());
      System.out.println("Hash code of work2: " + work2.hashCode());
    }
    catch(CloneNotSupportedException e)
    {
      System.out.println("Check your class implemented Cloneable interface. " + e);
    }
  }
}


Cloning Duplicate Object Marker Interface
Output screenshot on Cloning Duplicate Object Marker Interface

Observe, Worker class implements Cloneable interface.

Worker work2 = (Worker) work1.clone();

The above statement can be split into two for more clarity.

Object obj = work1.clone();
Worker work2 = (Worker) obj;

The clone() method returns an object of Object class. This is explicitly type casted to Worker. Now the objects work1 and work2 occupy different locations and there by encapsulation is maintained. For this reason, when work1 is changed work2 salary is not getting affected and similarly with work2 also. Observe the screenshot and the program.

As both objects occupy different locations, their hash codes are also different. Following are the requirements for cloning.

  1. Implement Cloneable interface
  2. Use clone() method of Object class
  3. After cloning, explicit casting is required

Marker Interface – Cloneable

Java API comes with six marker interfaces. An interface that does not contain any methods and variables is known as a marker Interface. For more explanation on marker interfaces and their list, refer I/O streams.

Array Cloning

Arrays are predefined objects in the Java language itself. So, like any other object, arrays also can be cloned. Following program illustrates.

public class Demo
{
      public static void main(String args[]) 
      {
            int x1[] = { 10, 29, 30, 40 };
            int x2[] = x1.clone();    

           System.out.println(x2[0]);       // prints 10
                        // now changing x1 value does not affect x2
           x1[0] = 100;
           System.out.println(x2[0]);      // printgs same 10
      }
}

Java Made Clear: List of 7 Marker interfaces

16 thoughts on “Cloning Duplicate Object Marker Interface”

  1. sir i hav a situation
    public class A
    {
    int x;
    A()
    {
    x=0;
    }
    A(int i)
    {
    x=i
    }
    public String toString()
    {
    return “x is”+x
    }
    }
    class B
    {
    public static void main(String args[])
    {
    A a1=new A(6);
    A a2=(A)a1.clone;// in this im not able to clone
    }
    }

    1. Try like this:

      public class A implements Cloneable
      {
      int x;
      A()
      {
      x=0;
      }
      A(int i)
      {
      x=i;
      }
      public String toString()
      {
      return “x is ” + x;
      }
      public static void main(String args[]) throws CloneNotSupportedException
      {
      A a1= new A(6);
      A a2= (A) a1.clone();
      System.out.println(a2);
      }
      }

  2. Sir,

    In the above explanation you have mentioned that,
    Java API comes with six marker interfaces.
    I know below marker interfaces,
    Cloneable, serializable, Remote & EventListener
    Can you please let us know other two?

  3. sir in this page’s 1st program you have type casted this
    “Worker work2 = (Worker) work1.clone();”

    sir here you are type casting super(Object) class object to sub(Worker) class object but this is against the law because we have to first assign (cast) sub(Worker) class object to super(Object) class object like

    Object r=new Worker();
    Worker work2 = (Worker) work1.clone();

    as you have said in
    http://way2java.com/casting-operations/object-casting/

Leave a Comment

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