DWQA Ask QuestionCategory: Questionsquestion-how-to-stop-object-cloning-in-java
admin Staff asked 5 years ago

Explain what is cloning? Ways to Stop Cloning in java?

2 Answers
Bindiya Patil Staff answered 5 years ago

Ans: Clone() is method in Java. object cloning is refer to creation of exact copy of an object. It creates a new instance of the class of current object and initializes all its fields with exactly the contents of the corresponding fields of this object. We can avoid cloning by giving already created object by clone method .

Chintan answered 5 years ago

The object cloning is a way to create exact copy of an object. The clone() method of Object class is used to clone an object.
The java.lang.Cloneable interface must be implemented by the class whose object clone we want to create. If we don\’t implement Cloneable interface, clone() method generates CloneNotSupportedException. public
Object clone() throws CloneNotSupportedException