Object cloning

The object cloning is a way to create exact copy of an object. clone() method of Object class is used to clone an object, in this case any changes you make to object-1 will reflect in object-2 .

Example:

class  MyString implements java.lang.Cloneable
{
    String s;
	
	MyString(String s){
	this.s=s;
	}
	void DoUpper()
	{
	 this.s=this.s.toUpperCase();
	}
	
	void DoLower()
	{
		this.s=this.s.toLowerCase();	
	}	
	void ShowString()
	{
	 System.out.println(this.s);
	}
	public Object clone()throws CloneNotSupportedException{  
		return super.clone();  
}
}
public class soft 
{	
	public  static void main(String  args[])throws CloneNotSupportedException
	{
		
		MyString i=new MyString("Softaid");
		i.DoUpper();
		i.ShowString();
		
		MyString p= (MyString)i.clone();
    
		p.ShowString();	
		
	}

}

Output

SOFTAID
SOFTAID