We can copy an object in 3 different ways. They are -
Shallow copy – It is a
bit-wise copy of an object. A new object is created that has an exact copy of
the values of the original object. If any of the fields of the object refers to
other objects, just the reference addresses are copied i.e., only the memory
address is copied.
Let us
take an example –
class A
{
int i;
int j;
}
A obj = new A();
obj.i=1;
obj.j=2;
A obj1 = obj; // Line 1
Here in
the above comment section Line 1 we are not creating a new object, instead we
are creating a reference variable which
is pointing to the object of obj in the heap. So in this situation, if we
change the value of obj then obj1 will also get reflected. So inside our heap
there will be one object, and inside stack we will have two reference pointing
to same object in the heap.
Example –
public class
Clon {
int i;
int j;
@Override
public
String toString() {
return "i = "+i+" j =
"+j;
}
public static void
main(String[] args) {
Clon obj
= new Clon();
obj.i=10;
obj.j=20;
Clon obj1
= obj;
System.out.println(obj);
System.out.println(obj1);
obj1.i=30;
System.out.println(obj); //
both gets reflected
System.out.println(obj1);
}
}
Output –
i = 10 j = 20
i = 10 j = 20
i = 30 j = 20
i = 30 j = 20
Deep Copy – a deep
copy copies all fields and makes copies of dynamically allocated memory pointed
to, by the fields. A deep copy occurs when an object is copied along with the
objects to which it refers. In deep copy we will create a new object and
manually we will copy one-one value from one object to another.
Let us
take an example –
class A
{
int i;
int j;
}
A obj = new A();
obj.i=1;
obj.j=2;
A obj1 = new A();
obj1.i = obj.i;
obj1.j=obj.j;
In the
above example even if any object value gets changed it doesnot reflect to the
existing value of another object. Suppose if we have thousands of variables,
then it’s not possible to go with deep copy.
Example –
public class
Clon {
int i;
int j;
@Override
public
String toString() {
return "i = "+i+" j =
"+j;
}
public static void
main(String[] args) {
Clon obj
= new Clon();
obj.i=10;
obj.j=20;
Clon obj1
= new Clon();
obj1.i=obj.i;
obj1.j=obj.j;
System.out.println(obj);
System.out.println(obj1);
obj1.i=30;
System.out.println(obj);
// i value not reflected
System.out.println(obj1);
}
}
Output –
i = 10 j = 20
i = 10 j = 20
i = 10 j = 20
i = 30 j = 20
Cloning – Cloning
is the combination of deep copy and shallow copy. Here we don’t have to create
object, clone will automatically create the object and it will copy all the
values into the new object. To clone an object simply we are not allowed, we
have to give some permissions using Cloneable interface which means the class
should implements cloneable interface and overrides clone() method. Cloneable
interface is a marker interface without any method declaration.
Example –
public class
Clon implements Cloneable{
int i;
int j;
@Override
public
String toString() {
return "i = "+i+" j =
"+j;
}
public
Object clone() throws CloneNotSupportedException{
return super.clone();
}
public static void
main(String[] args) throws CloneNotSupportedException {
Clon obj
= new Clon();
obj.i=10;
obj.j=20;
Clon obj1
= (Clon)obj.clone();
System.out.println(obj);
System.out.println(obj1);
obj1.i=30;
System.out.println(obj);
System.out.println(obj1);
}
}
Output –
i = 10 j = 20
i = 10 j = 20
i = 10 j = 20
i = 30 j = 20
No comments:
Post a Comment