InterviewSolution
| 1. |
What is cloning? What is the difference between shallow cloning and deep cloning? |
|
Answer» Cloning means creating a copy of an object or creating a duplicate object - the state of them should be the same. An object may be composed of SEVERAL other OBJECTS. Shallow cloning creates a new object and assigns its field values to the corresponding fields of the new object. As the fields contain only the references of the objects which reside in the HEAP, fields of the new object also point to the same component instances. Shallow cloning is fast but has a serious downside in that if any of the component objects is changed, it REFLECTS in the cloned object as well, because both of them holds the references of the same objects. Deep Cloning, on the other side, doesn’t copy the references from the fields, it creates a duplicate of the component objects as well. As in Deep cloning, all the component objects are cloned it’s comparatively slow but creates a true duplicate of the actual object. |
|