Pages

How to clone a java object ???

**********************************************************
COMPLETE SOURCE CODE FOR : CloneObjectInJava.java
**********************************************************


//Class for object that you want to clone must overwrite method clone() first

public class CloneObjectInJava implements Cloneable
{
public void printAText()
{
System.out.println("HI");
}

//OVERWRITE METHOD CLONE
public Object clone()
{
Cloneable makeClone=new CloneObjectInJava();
return makeClone;
}

public static void main(String[]args)
{
//Create a CloneObjectInJava object name "coij"
CloneObjectInJava coij=new CloneObjectInJava();

try
{
//Create clone for "coij"
CloneObjectInJava coijClone=(CloneObjectInJava)coij.clone();
//Call method printAText using clone
coijClone.printAText();
}
catch(Exception exception)
{
exception.printStackTrace();
}
}
}


**********************************************************
JUST COMPILE AND EXECUTE IT
**********************************************************
Related Posts Plugin for WordPress, Blogger...