Cloning is a concept used to create a new object of the existing object. It means creating another object in the memory same as the existing one by copying the content of it.

For cloning a object java vendor has provided a method i.e clone() method in the Object class which is used to clone the object.

Clone() method is defined as protected in the Object and return the Object class object. Signature of the clone() is :-

protected Object clone() throws CloneNotSupportedException

This method throws one exception i.e java.lnag.CloneNotSupportedException, that means we need to handle this exception while using clone() in our program or we need to define the same exception with the throw keyword in the sub-class. 

We need to implement Cloneable interface to that class whose object we want to clone because by implementing JVM will got to know that this class is to be cloned.Syntax for Cloneable interface :

public interface java.lang.Cloneable{}

Cloneable interface is a marker interface that means without any method or member defined or we can say that empty interface. 

There are two types of cloning possible in java

1. Shallow Cloning ( default Cloning )
2. Deep Cloning ( Manual Cloning ) 

Shallow Cloning 

It is the Default implementation of the clone() method in the Object class. If we are using clone() method then it do shallow cloning. It is used to clone the data members of the existing class but fails to clone any of the member object. To overcome this limitation of the Shallow cloning there is a concept given by the java vendor is deep cloning. 

Deep Cloning 

It is manual implementation which has to be done by user itself. As not predefined method is available so we need to write our own code to do deep cloning and it is used to fulfill the limitations of the shallow cloning as it fails to clone the object member.   

Related Posts:

  • Dynamic Dispatch in Java It is a concept in java to achieve the Dynamic / Run-time polymorphism. It is the process of assigning sub-class object in super type reference later if needed that sub-class object can be type-caste into its super type. Dy… Read More
  • Interface in Java Interface is a keyword in java and also a concept in java to over come the limitation of not supporting multiple inheritance by java. Now with the concept of interface we can have implement multiple interface full filing th… Read More
  • Abstract keyword in java Abstract is a keyword in java. So its a reserved word in java. general meaning of abstract is to hide the implementation, only show the outer structure. Now in computer science its meaning is that user can have their own i… Read More
  • Abstract class in java When we use an abstract modifier before the name of any user-define class then it is called as an abstract class. Abstract class in java has only method signature whose implementation must be in its sub-classes. Importan… Read More
  • Accessibility of access modifiers in java Read More

0 comments:

Post a Comment

Ads 468x60px

.

Ads

.

Featured Posts

Popular Posts

Like Us On FaceBook

Total Pageviews

201978

Online Members

Live Traffic