What is the role of the clone() method in Java?

protected Object clone() throws CloneNotSupportedException - this method is used to create a copy of an object of a class which implements Cloneable interface.
class demo implements Cloneable
{ int x=10;
void display(){System.out.println(x);}
public static void main(String[] args)throws Exception{
demo d=new demo();
demo obj=(demo)d.clone();
obj.display();
}}If the class on which the clone() method is called, that should implement Cloneable interface otherwise it throws CloneNotSupportedException.

No comments:

Post a Comment