Way2Java

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);
    }
  }
}



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