hamburger

Difference between Shallow and Deep Copy of a Class

By BYJU'S Exam Prep

Updated on: September 25th, 2023

Difference between Shallow and Deep Copy of a Class: Shallow Copy and deep Copy are related to the cloning process, so we must first understand the difference between Shallow and Deep Copy of a Class and then div individually what is Shallow Copy and deep Copy of a class.

The difference between a shallow and deep copy of a class is that a shallow copy creates a new compound object and then (to the greatest extent possible) inserts references to the original’s objects into it. In contrast, the deep Copy creates a new compound object and recursively inserts copies of the original’s objects into it.

Download Formulas for GATE Computer Science Engineering – Programming & Data Structures

Difference between Shallow and Deep Copy of a Class

We will discuss the functionality of the Difference between shallow and deep copy of a class individually. The differences are based on the declaration, type, etc., which are also important for the GATE exam.

Key differences between Shallow and Deep Copy of a Class

Shallow Copy

Deep Copy

Shallow Copy stores a copy of the original object and refers to it.

Deep Copy stores the original object’s Copy and recursively copies the objects.

Shallow Copy saves object references to the original memory address.

Deep Copy keeps multiple copies of the object’s value.

A shallow copy costs less.

Deep Copy is extremely costly.

Shallow Copy is quick because no new memory is allocated.

Deep Copy is slow because new memory is being allocated.

The clone() method’s default implementation supports shallow Copy.

To enable deep copy support in the clone() method, override the clone() method.

What is a Shallow Copy?

A shallow copy is the creation of a new collection object (such as our list / dict / set), followed by the addition of references to the original object’s child objects. Shallow copies have only one level of depth. Shallow copies can be used to formulate questions in the GATE question paper. Because the shallow copying process is not recursive, no copies of child objects are created.

This may appear complicated, but the crux is that in the case of shallow copy, any changes made to the copy of the original object will also be reflected in the original object.

What

Download Formulas for GATE Computer Science Engineering – Algorithms

What is Deep Copy?

A deep copy is the inverse of a shallow copy. In this case, the deep copy process is recursive. As in shallow copy, we create a new collection object and recursively add copies of the original’s child objects. This allows us to create a fully autonomous clone of the original object. The deep copy is an important part of the  GATE CSE syllabus. The problem is that any changes we make to the copy will not be reflected in the original.

What

Download Formulas for GATE Computer Science Engineering – Digital Logic

Important GATE Topics

Structural Steel Statically Indeterminate
RLC Circuit Work Done By A Force
Structural Analysis Motion Under Gravity
Inductors In Series Dynamic Resistance
Method Of Joints Static Resistance
Our Apps Playstore
POPULAR EXAMS
SSC and Bank
Other Exams
GradeStack Learning Pvt. Ltd.Windsor IT Park, Tower - A, 2nd Floor, Sector 125, Noida, Uttar Pradesh 201303 help@byjusexamprep.com
Home Practice Test Series Premium