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

Deep Copy and Shallow Copy

Uploaded by

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

Deep Copy and Shallow Copy

Uploaded by

saadbink1234
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 19

Object Oriented

Programming - Java
Instructor: Afifa Hameed
Copy Constructor
• In Java, a copy constructor is a special type of constructor that creates an object using
another object of the same Java class. It returns a duplicate copy of an existing object of
the class.

• We can assign a value to the final field but the same cannot be done while using the
clone() method. It is used if we want to create a deep copy of an existing object. It is
easier to implement in comparison to the clone() method.

• The primary use case of a copy constructor is to create a new object with the same
state as an existing object. It is particularly useful in scenarios where we need to
duplicate an object while maintaining its integrity. By leveraging a copy constructor, we
ensure that the new object is independent of the original one, thereby preventing
unintended side effects that might occur with shallow copying.
Use of Copy Constructor

We can use the copy constructor if we want to:

• Create a copy of an object that has multiple fields.


• Generate a deep copy of the heavy objects.
• Avoid the use of the Object.clone() method.
Creating a Copy Constructor

public class Fruits


{
private double price;
private String name;
//copy constructor
public Fruits(Fruits fruits)
{
//getters
}
}
Copy each field (variable) object into the newly created
instance

public class Fruits


{
private double price;
private String name;
//copy constructor
public Fruits(Fruits fruits)
{
//copying each filed
this.price = fruits.price; //getter
this.name = fruits.name; //getter
}
}
Deep versus Shallow Copying

• Copy constructors allow developers to choose between deep and shallow copying
strategies based on the requirements of the application.

• While shallow copying may suffice for simple objects with primitive fields, deep
copying is essential for preserving the integrity of complex objects with
references to mutable state.
Shallow Copy in Java
• When we do a copy of some entity to create two or more than two
entities such that changes in one entity are reflected in the other
entities as well, then we can say we have done a shallow copy.

• In shallow copy, new memory allocation never happens for the other
entities, and the only reference is copied to the other entities.
Shallow Copy Example
Output of Shallow Copy
Deep Copy
Output of Deep Copy
Array of objects in Java
• We know that an array is a collection of the same data type that
dynamically creates objects and can have elements of primitive types.
• Java allows us to store objects in an array.
• In Java, the class is also a user-defined data type.
• An array that contains class type elements are known as an array of
objects.
• It stores the reference variable of the object.
Array of objects illustration
Creating an Array of Objects

• Before creating an array of objects, we must create an instance of the


class by using the new keyword.
• We can use any of the following statements to create an array of
objects:
ClassName obj[]=new ClassName[array_length]; //declare and instantiate an array of objects
OR
ClassName[] objArray;
OR
ClassName objeArray[];
Array of objects Example
Output of Array of Object Example
Java Clone() method
• The clone() method in Java is used to create a copy of an object. It is
defined in the Object class and provides a shallow copy of the object.
• For it to work, a class must implement the Cloneable interface and
override the clone() method.
Key points about Clone() method
• Shallow Copy: By default, the clone() method creates a shallow copy,
meaning it copies the fields of the object but not the objects
referenced by those fields.

• Cloneable Interface: If the class does not implement the Cloneable


interface, calling the clone() method will throw a
CloneNotSupportedException.

• Deep Copy: For deep cloning, you must override the clone() method
and implement custom logic to copy all referenced objects.
Home task!
• Explore the Java Clone() method.

You might also like