Interview Questions

What is the difference between deep copy and shal­low copy?

Java Interview Questions for Cloning and Cloneable


(Continued from previous question...)

What is the difference between deep copy and shal­low copy?

When a class implements Cloneable interface but does not override the clone method then the shallow copy is created for that object. In shallow copy, all the member variables except member instance variables are copied from the source to clone. The instance member variables of the source and the clone will keep on pointing to the same object.

Deep copy of an object is created when the class overrides the clone method and the instance member variables of the clone are initialized so that they point to different objects than those pointed by the source object.

(Continued on next question...)

Other Interview Questions