0% found this document useful (0 votes)
6 views

Assignment OOP

Deep copy creates an independent copy of an object or data structure that can be modified without affecting the original. It copies everything, including nested objects and data structures, so that the copy has a separate memory location from the original. This allows safe modification of the copy. The example in Java creates a CloneExample object, makes a deep copy using the clone() method, and shows the copy can be modified independently without changing the original.

Uploaded by

Syed Salar Xhah
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

Assignment OOP

Deep copy creates an independent copy of an object or data structure that can be modified without affecting the original. It copies everything, including nested objects and data structures, so that the copy has a separate memory location from the original. This allows safe modification of the copy. The example in Java creates a CloneExample object, makes a deep copy using the clone() method, and shows the copy can be modified independently without changing the original.

Uploaded by

Syed Salar Xhah
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

What is Deep copy, purpose of Deep copy?

Explanation of Deep Copy


also write an example in java.
Deep Copy:
Deep copy is a way of making an exact copy of something in programming so that any changes
made to the copy don't affect the original. It copies everything, including anything inside the
original, so that the copy is completely separate and independent. This is useful when working
with complex data structures.

Purpose of Deep Copy:

 The purpose of deep copy in programming is to create an independent copy of an object


or data structure that can be modified without affecting the original.

 Deep copy is particularly useful when working with complex data structures that contain
references to other objects or data structures. Without deep copy, changes made to a
copy of a complex data structure could inadvertently affect the original data structure.

 The purpose of deep copy is to create a completely independent copy of an object or


data structure to allow for safe and reliable modification without affecting the original.

 One main fact of deep copy is that it is commonly used in object-oriented programming
to create a new instance that has the same values as an existing instance but with a
separate memory location. This is important because it allows for safe and independent
modification of the new instance without affecting the original, improving the reliability
of the program.

Explanation of Deep copy:


Whenever we need our own copy not to use default implementation we call it is a deep copy,
whenever we need a deep copy of the object we need to implement it according to our need.
So, for a deep copy, we need to ensure all the member classes also implement the Cloneable
interface and override the clone () method of the object class.
A deep copy means actually creating a new array and copying over the values.

Example in java:
import java.util.Scanner;
public class CloneExample implements Cloneable {
private String name;
private int age;
public CloneExample(String name, int age){
this.name = name;
this.age = age;
}
public void displayData(){
System.out.println("Name : "+this.name);
System.out.println("Age : "+this.age);
}
public static void main(String[] args) throws CloneNotSupportedException {
Scanner Salar =new Scanner(System.in);
System.out.println("Enter your name ");
String name = Salar.next();
System.out.println("Enter your age ");
int age = Salar.nextInt();
CloneExample std = new CloneExample(name, age);
System.out.println("Contents of the original object");
std.displayData();
System.out.println("Contents of the copied object");
CloneExample copiedStd = (CloneExample) std.clone();
copiedStd.displayData();
}
}

You might also like