Show Mobile Navigation

Webring

Powered by WebRing.

Complete guide on cloning in Java

Sachin R Kukale - 20:37
‘Cloning’ means creating new objects using the already existing objects in the memory. Cloning does not involve assigning the reference of already existing to the new reference variable. It is the process of creating brand new object but by using the objects which are already present in the memory.
The ‘clone’ method is present in the java.lang.Object class, which is super class for every class defined in Java source code as:
protected native java.lang.Object clone() throws java.lang.CloneNotSupportedException;
This method is declared as protected which means that you can call this method for subclass which are outside the package also using current sub-class object. This method is implemented in non-java language and so it is also declared as ‘native’.
In case, when you are required to access ‘clone’ method in other subclass then you have to override the clone method in your class. This restriction is to avoid, letting other users to create clones of objects which are created by you.
You can define your own custom implementation or you can use implementation available in the super class to clone your objects after overriding.
In case you are cloning your object with a super class implementation then that implementation must throw an exception java.lang.CloneNotSupportedException and you have to use ‘super’ keyword from the overridden method.
If you are cloning the object using the super class implementation with ‘super’ keyword then your class must be subtype of ‘cloneable’ interface, which is marker interface. If this condition is not satisfied by your class then JVM will throw ‘CloneNotSupportedException’.
Marker interface is used to instruct JVM to perform some specific tasks according to the marker interface on your class.
While cloning the object using the super class implementation constructor of your class will not be used to create the instance but the native implementation will copy whole object to the new location in memory and that location address will be returned.
There are two types of cloning:
        i.            Shallow Cloning: Shallow cloning is the default implementation for cloning the objects.
Shallow cloning process of cloning is performed only current object and references of any other objects which are in current object will not be cloned.
Also modifying the property of current object will not affect the propertied of the cloned object.
In shallow cloning modifying properties of member object will affect the cloned object.
      ii.            Deep Cloning: Deep cloning is the process of cloning the current object as well as the member objects. In other words deep cloning involves cloning on current object and on all the objects for which references are held by the current object.
Modifying the property of any object will not affect the property of cloned object in deep cloning.
There is not implementation provided for deep cloning in Java and if you want to perform deep cloning then you have to write your own implementation.



0 comments:

Post a Comment