What is clone method in Java ?
Posted by Paul on April 27th 2018 13:25 PM
clone() method in Java : The process of creating same copy of an object is known as cloning. 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. - The class whose object’s copy is to be made must have a public clone method in it or in one of its parent class. - Every class that implements clone() should call super.clone() to obtain the cloned object reference. - The class must also implement java.lang.Cloneable interface whose object clone we want to create otherwise it will throw CloneNotSupportedException when clone method is called on that class’s object. Syntax of clone() method : protected Object clone() throws CloneNotSupportedException Here is the code to implement clone() method :