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

Answer Posted / 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       View All Answers


Please Help Members By Posting Answers For Below Questions

Which of the classes will have more memory allocated?

534


Explain public static void main(string args[]).

564


Why vector is used in java?

554


Can we create an object of private class?

543


What class allows you to read objects directly from a stream?

866






Difference between predicate, supplier and consumer ?

573


What are the two ways of implementing multi-threading in java?

810


What is a treeset in java?

542


Can private class be inherited in java?

547


What’s the difference between constructors and other methods?

529


Write the algorithm to check the number non-leaf nodes in a tree.

587


Write a program to find the whether a number is an Armstrong number or not?

571


What are the library functions in java?

542


Is java platform independent?

563


Is class forname reflection?

514