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

Can we create object of inner class in java?

0 Answers  


What is an example of character?

0 Answers  


Suppose there is a System A which gives some output. This output is used as input by system B. The rate at which System A produces is faster than the rate at which system B consumes it. How can you improve this?

1 Answers   RBS,


i don't want fullforms of JDK an JVM i want definitions for them

2 Answers  


What is the main difference between java platform and other platforms?

0 Answers  






What is the final variable?

0 Answers  


What is json parser in java?

0 Answers  


What are legal modifiers that we can use to declare an inner class?

1 Answers   Infosys,


What do you understand by the bean persistent property?

0 Answers  


What is meant by constructor?

8 Answers  


What is jpa specification?

0 Answers  


Why is logger singleton?

0 Answers  


Categories