Cloning Duplicate Object Marker Interface

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


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.

Java Made Clear: List of 7 Marker interfaces

16 thoughts on “Cloning Duplicate Object Marker Interface

  1. manik

    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. S. Nageswara Rao, Corporate Trainer

      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. Gopikrishnan

    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. v.s.

    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 Reply

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