what is object deep copy and shallow copy and why it is
required?

Answers were Sorted based on User's Feedback



what is object deep copy and shallow copy and why it is required?..

Answer / sanjay

Java provides a mechanism for creating copies of objects
called cloning.
There are two ways to make a copy of an object called
1. shallow copy
2. deep copy

Shallow copy is a bit-wise copy of an object. A new
object is created that has an exact copy of the values in
the original object. If any of the fields of the object are
references to other objects, just the references are copied.
Thus, if the object you are copying contains references to
yet other objects, a shallow copy refers to the same subobjects.

Deep copy is a complete duplicate copy of an object.
If an object has references to other objects, complete new
copies of those objects are also made. A deep copy generates
a copy not only of the primitive values of the original
object, but copies of all subobjects as well, all the way to
the bottom. If you need a true, complete copy of the
original object, then you will need to implement a full deep
copy for the object.
Java supports shallow and deep copy with the
Cloneable interface to create copies of objects. To make a
clone of a Java object, you declare that an object
implements Cloneable, and then provide an override of the
clone method of the standard Java Object base class.
Implementing Cloneable tells the java compiler that your
object is Cloneable. The cloning is actually done by the
clone method.

Is This Answer Correct ?    11 Yes 0 No

what is object deep copy and shallow copy and why it is required?..

Answer / amit singh

hi i'm amit
now i've one hour right now to tell you what is the cloning
imagine you create a object Like (Amit a = new Amit();)
and the you ggive asingn the paticular ref variable in the
other refernce variable
like( Amit b = a;) is this is cloning not
Why: because the refernce variable only to get the bitwise
patter the way to get the onject where it is
so its not the cloning.
so what you must to do if you want to create a clone of
thsis object then what to do you should to implements the
cloneable interface for that class you want to clone.
so implemnts the cloneable interface.
and override the clone method of the Object class

/*imagine a girl is so birliant i want to this girl
i want to this girl but how so i'm going to create the
clone of
this girl,beacuse i'm not able to approach directly
beacuse i don't want any reference to get the way to meet
the
original apoorv beacuse i'm so shy
*/

SHALLOW COPY:

class Apoorva implements Cloneable
{
private String s;
private int age;
public Apoorva(String s1,int age)
{
s = s1;
this.age = age;
}

public String getthis()
{
return s + " " + age;
}

public Object clone() throws CloneNotSupportedException
{

return super.clone();

}
}

public class Amitsingh
{
public static void main(String []args)
{
Apoorva a = new Apoorva("she is birliant",21);
Apoorva b=null;

try{
b = (Apoorva)a.clone();
}
catch(Exception e)
{
}
System.out.println("hi apporv " + b.getthis() + " i mean
it " + a.getthis());
}
}

/*so what you get Object.clone() omnly do the simple
cloning
its only create a new object and then copy the value of
field
means it copy all field from one object to another.
but what when you have a meber of any class type then
if you write code which above mention will only copy the
refence of the
the class type which will be member in this particular
class
so that why itis called a shallow copy io field to field
copy.
note bydefault it does fiel by field copy only primitive
then the new object will create and the referance to the
object will return
but if there is any Class type (forget that i will use
above a
String class type because it is immutable so don't matter)

which is immutable then object refrensce to those member
will copy
both refrence refer to the same object.

so "MEANS TO SAY THAT IN SHALOOW COPY STRING AND
PRIMITIVE TYPES ITS NOT PROBLEM BUT
WHEN MUTABLE THEN WHAT TO DO IS THE SAHLLOWCOPY WILL WORK

ANSWER IS NO, IMAGINE YOU HAVE A CLASS A IN WHICH
A MEMBER OF ANOTHER CLASS B TYPE AND YOU WANT TO
CLONE THE CLASS AND WANT TO CHANGE THE VALUE OF
ANY MEMBERFIELD WHICH IS IN THE CLASS B ITS POSSIBLE
THROUGH TO CLONE THE OBJECT OF CLASS A USING SHALLOW COPY"

not its not possible but why answer is only the reference
variable will copy of class type not the whole class so the
after cloning using sahllowopy
you have the refernce of same object bso
it will not work for you because you want to change in
member field value in the object of class B
which will create during the cloning.but the class B object
not cloned during the shallow copy.
"so if you changed that you are in dream you think you
cloned the Class B
and you changed its state so the original object state will
also changed
so you don't want to that then the deepcloning comes in a
light.
*/

so now the turns to understand the deep copy


DEEP COPY:

class Itsection implements Cloneable
{
private int passoutyear;
private String Floor;
public Itsection()
{
}
public Object clone() throws CloneNotSupportedException
{
return super.clone();
}

public void setall(int x,String s)
{
passoutyear=x;
Floor = s;
}

public String gets()
{
return " " + Floor + " ";
}
public String getint()
{
return " " + passoutyear + " ";
}
}



// class in which other class type member to be cloned

class Apoorva implements Cloneable
{
private String s;
private int age;
private Itsection it; //class type must implement the
cloneable interface for deep copy
//otherwise we didn’t able
to call the protected method Object clone()
Apoorva(String s1,int age)
{
this.s = s1;
this.age = age;
it = new Itsection();
}
public String getthis()
{
return s + " " + age;
}

public Object clone()
{
Apoorva a1=null;
try
{
a1 = (Apoorva)super.clone();
a1.it = (Itsection)a1.it.clone();

}

catch(CloneNotSupportedException e)
{
}
return a1;
}

public static void main(String []args)
{
Apoorva a2 = new Apoorva("she is birliant",21);
a2.it.setall(2009,"second floor 2nd buiding");
System.out.println("original object" + a2.it.gets() +
a2.it.getint());


Apoorva a3 =(Apoorva)a2.clone();
a3.it.setall(2010,"trd floor 2nd building");
System.out.println("clone object " + a3.it.gets() +
a3.it.getint());
System.out.println("hi apporva " + a2.getthis() + " i
mean it " + a3.getthis());

}
}

thanks AMIT
SINGH
amitsing2008@gmail.com

don't arg wthout run the code
amit09mca

Question:- to clone the thread object how itis poosible or
not??????????
ans: ---
to clone innerclasses it is possible or
not?????????????/////
bye

Is This Answer Correct ?    7 Yes 0 No

Post New Answer

More Core Java Interview Questions

What is thread pool in java with example?

0 Answers  


How to excute - Interface - Inner class- method can any one tell how to execute/ call this main method public interface abc { static int i=0; void dd(); class a1 { a1() { int j; System.out.println("inside"); }; public static void main(String a1[]) { System.out.println("in interfia"); } } }

1 Answers  


Can you tell me range of byte?

0 Answers  


Explain JMS in detail.

0 Answers   Amdocs,


Is null a string in java?

0 Answers  






what are the application of compiler and interpreter for source program

2 Answers  


What are the concepts of 'OOPS'?

0 Answers   Atos Origin,


Write java code to print "Hello how are you" Thread1 should have "Hello" Thread2 should have "how are you" both the threads should start at the same time

4 Answers   Huawei,


How to decrease number of hashings in has

1 Answers  


Write a program in java to establish a connection between client and server?

0 Answers  


What is data type modifier?

0 Answers  


What is polymorphism java example?

0 Answers  


Categories