Home » Core Java » Shallow Cloning in Java

Shallow Cloning in Java

 

Shallow Cloning or Shallow Copy:

Before Explaining Shallow copy ,if  you are new to cloning concepts,please go though the link Object cloning in java to understand cloning.

1 : The default behavior of an object’s clone() method yields a shallow copy.

2 : Shallow copy  returns ‘Object’ as type and which needs to explicitly cast back to original object.

3 : Class whose object needs to be cloned should implement Cloneable marker interface to avoid getting CloneNotSupportedException. 

4 : Class whose object needs to be cloned  must overwrite clone() method  to provide its own meaning or invoke the super.clone() to provide Object class’s implementation.

5 : copy constructors provided by various collection classes has shallow copy implementation

Program :

 
 


Leave a comment

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

seventeen + 4 =