GeekInterview.com
   Home |  Tech FAQ  |   Interview Questions |  Placement Papers |  Tech Articles |  Learn |  Freelance Projects |  Online Testing |  Geeks Talk |  Job Postings |  Knowledge Base | Site Search |  Add/Ask Question

  GeekInterview.com  >  Tech FAQs  >  Programming  >  Java

 Print  |  
Question:  

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?





June 06, 2005 06:55:26 #1
 KV   Member Since: Visitor    Total Comments: N/A 

RE:

As of now ,what i know abt cloning is that we can make a field to field copy of an object.ie when


 
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. 
     

 

Back To Question