The clone() method is used to create a copy of any class which implements the Clonable interface. If the class does not implement the Clonable interface and you still call clonable on that then it will throw ClassNotSupportedException. When we call this clone method then the copy of primitive types will be created but object references will be copied as they are. So basically original and cloned object will have the same object references. So modification in the cloned object will be reflected in the original object.
Shallow copy example,
If we need a deep copy of any object then we will need to implement the clone method manually,
Deep copy example,
No comments:
Post a Comment