How to clone the objects?

Showing Answers 1 - 6 of 6 Answers

pankaj

  • Mar 1st, 2006
 

to clone a object your class must implement Cloneable interface 

and then you can call  clone() method ob Object class on the object of the class which is implementing cloneable interface

  Was this answer useful?  Yes

elidoran

  • Mar 4th, 2006
 

You do the actual cloning inside a try-block catching the clone exception (which only happens if you aren't implementing the Cloneable interface). You call super.clone(); This provides a byte copy of the object but NOT a deep copy of any reference variables. For example, if your object contains an array of objects then both the original object and the clone contain an array that references the SAME objects. So, you'd need to do a deep copy of the array manually in the object's clone() method. You could simply iterate over the array and call clone() on those objects placing their clones in the base clone's array (Assuming they are Cloneable too).

  Was this answer useful?  Yes

funny

  • Sep 6th, 2007
 

In shallow cloning, any modifications done to the cloned object will also affect the original object whereas in deep cloning, any modifications done to the cloned object will not affect the original object.

  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions