Java Shallow & Deep cloning - Explain Shallow & deep cloning.

Explain Shallow and deep cloning.

Cloning of objects can be very useful if you use the prototype pattern or if you want to store an internal copy of an object inside an aggregation class for example.

Deep cloning - You clone the object and their constituent parts.

It should be used when it is inappropriate to separate the parts, the object is formed of, from it.

Shallow cloning - You clone only the object, not their parts. You add references to their parts.

It should be used when it is adequate to have the references added to the cloned object

What is Shallow and deep cloning in Java?

Cloning refers to creating duplicate copies of objects in java.

Shallow Cloning: Shallow cloning is a bitwise copy of an object. New object is created which is an exact copy that of the original one. In case any objects are referring the fields of these objects, just the references are copied.

Deep Cloning: In deep cloning, complete duplicate copy of the original copy is created. Deep cloning creates not only the primitive values of the original objects but also copies all its sub objects as well.

Clonable interface is used to perform cloning in java.

Java private constructor - Explain the impact of private constructor.
Java private constructor - Private Constructors can't be access from any derived classes neither from another class. .....
Java Static Initializers - What are static Initializers?
Java Static Initializers - A static initializer block resembles a method with no name, no arguments, and no return type...
Purpose of the wait (), notify (), and notifyAll() methods
notify() Wakes up a single thread that is waiting on this object's monitor...
Post your comment