GeekInterview.com
Series: Subject: Topic:
Question: 995 of 995

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?

Asked by: Interview Candidate | Asked on: Jun 25th, 2005
Showing Answers 1 - 2 of 2 Answers
KV

Answered On : 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. 

  
Login to rate this answer.
kjwilde

Answered On : Jun 22nd, 2009

View all answers by kjwilde

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.

  
Login to rate this answer.

Give your answer:

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

Related Open Questions

Ads

Connect

twitter fb Linkedin GPlus RSS

Ads

Interview Question

 Ask Interview Question?

 

Latest Questions

Interview & Career Tips

Get invaluable Interview and Career Tips delivered directly to your inbox. Get your news alert set up today, Once you confirm your Email subscription, you will be able to download Job Inteview Questions Ebook . Please contact me if you there is any issue with the download.