#826 – Deep Copies
April 19, 2013 6 Comments
When making a copy of an object, you can either make a shallow copy or a deep copy. As opposed to a shallow copy, a deep copy is one in which a copy is made not only of the top-level object, but of all the objects referenced by the original object.
During the copy process, whenever a reference to a child object is encountered, a new instance (or deep copy) of the child object is made.
For example, if a Dog object contains a reference to a DogCollar object and we make a deep copy, we get the following:
When doing a deep copy, a deep copy is typically made of all child objects in the object hierarchy. For example, if the DogCollar instance had in turn referred to another object, a copy would have been made of that object.