As of now ,what i know abt cloning is that we can make a field to field copy of an object.ie when we clone an object,its state as well as a its behaviour is copied to the cloned object.My question is what is the neccesity of it? cant we create another object of the same class? so how does its different from cloned object (that is creating another object of that class rather than  being cloned).is there any security issues coupled with this cloned object? And What is the Concrete purpose of Cloning?

Showing Answers 1 - 4 of 4 Answers

KV

  • Jun 25th, 2005
 

Cloning is necessary. Suppose one of the instance fields of your class is mutable object... so if you are wrting a accessor (getter) for such a field.. u can not return the field 
object itself... as once returned.. the calling method 
or class can tamper it..that ways you are breaking encapsulation in a way that u r giving access to your field which is indeed private... instead, you can return a clone object of the same... which wouldnt affect the state of the class.. even if its tampered with. 

  Was this answer useful?  Yes

kjwilde

  • Jun 22nd, 2009
 

Creating a new instance of the same class and then being sure to apply all attributes of the original object to the new object would be equivalent to cloning. The clone method takes the legwork out of getting a duplicate of an 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