Clone: Cloning means creating new object with the existing object state is called as cloning.
Simple Program to understand why cloning is required:
A.java
class A
{
int a = 111;
int b = 222;
public static void main( String[] clone )
{
A obj = new A();
System.out.println(" obj: "+obj.a+"..."+obj.b);
A obj1 = obj;
System.out.println(" obj1: "+obj1.a+"..."+obj1.b);
System.out.println(" obj: "+obj.hashCode() );
System.out.println(" obj1: "+obj1.hashCode() );
}
}
Program For Clonning
B.java
class B implements Cloneable
{
int a = 111;
int b = 222;
public static void main( String[] s)
throws CloneNotSupportedException{
B obj = new B();
System.out.println(" obj: " + obj.a +"..."+obj.b);
obj.a = 888;
obj.b = 999;
System.out.println(" obj: " + obj.a +"..."+obj.b);
B obj1 = new B();
System.out.println(" obj1: " + obj1.a +"..."+obj1.b);
Object o = obj.clone();
B obj2 = (B)o; // DownCasting
System.out.println(" obj2: " + obj2.a +"..."+obj2.b);
System.out.println("\n\n obj: "+obj.hashCode());
System.out.println(" obj1: "+obj1.hashCode());
System.out.println(" obj2: "+obj2.hashCode());
}
}
Student.java
class Address{
String cityName = "Hyderabad";
String stateName = "Telangana";
}
class Student implements Cloneable{
int sid = 101;
String sname = "ram";
int sage = 29;
Address addr = new Address();
public static void main( String args[] )
throws CloneNotSupportedException{
Student s1 = new Student();
System.out.println( s1.sid );
System.out.println( s1.sname );
System.out.println( s1.sage );
System.out.println( s1.addr.cityName );
System.out.println( s1.addr.stateName );
s1.sid = 111;
s1.sname = "Atharva";
s1.sage = 30;
s1.addr.cityName = "Dhule";
s1.addr.stateName = "Maharashtra";
System.out.println("---------S1 Details-----------");
System.out.println( s1.sid );
System.out.println( s1.sname );
System.out.println( s1.sage );
System.out.println( s1.addr.cityName );
System.out.println( s1.addr.stateName );
Student s2 = (Student)s1.clone();
System.out.println("---------S2 Details-----------");
System.out.println( s2.sid );
System.out.println( s2.sname );
System.out.println( s2.sage );
System.out.println( s2.addr.cityName );
System.out.println( s2.addr.stateName );
System.out.println("\n\n"+s1.hashCode()+" "+s2.hashCode());
// If address of anyone memory location got change then it will reflect
// to both.
s1.addr.cityName = "chennai";
System.out.println( s1.addr.cityName );
System.out.println( s2.addr.cityName );
}
}
Student.java
class Address extends java.lang.Object implements Cloneable{
String cityName = "Hyderabad";
String stateName = "Telangana";
public Address clone() throws CloneNotSupportedException{
return (Address)super.clone();
}
}
class Student extends java.lang.Object implements Cloneable{
int sid = 101;
String sname = "ram";
Address addr = new Address();
public Student clone() throws CloneNotSupportedException
{
Student s3 = (Student) super.clone();
s3.addr = this.addr.clone();
return s3;
}
public static void main( String args[] )
throws CloneNotSupportedException{
Student s1 = new Student();
Student s2 = s1.clone();
System.out.println("\n\n S1: \n"+s1.sid );
System.out.println( s1.sname );
System.out.println( s1.addr.cityName );
System.out.println( s1.addr.stateName );
System.out.println("\n\n S2: \n"+s2.sid );
System.out.println( s2.sname );
System.out.println( s2.addr.cityName );
System.out.println( s2.addr.stateName );
s2.addr.cityName = " Dhule ";
s2.addr.stateName = " Maharashtra ";
System.out.println("\n\n S1: " + s1.addr.cityName );
System.out.println( s1.addr.stateName );
System.out.println("\n\n S2: " + s2.addr.cityName );
System.out.println( s2.addr.stateName );
}
}
Simple Program to understand why cloning is required:
A.java
class A
{
int a = 111;
int b = 222;
public static void main( String[] clone )
{
A obj = new A();
System.out.println(" obj: "+obj.a+"..."+obj.b);
A obj1 = obj;
System.out.println(" obj1: "+obj1.a+"..."+obj1.b);
System.out.println(" obj: "+obj.hashCode() );
System.out.println(" obj1: "+obj1.hashCode() );
}
}
Output:
Program For Clonning
B.java
class B implements Cloneable
{
int a = 111;
int b = 222;
public static void main( String[] s)
throws CloneNotSupportedException{
B obj = new B();
System.out.println(" obj: " + obj.a +"..."+obj.b);
obj.a = 888;
obj.b = 999;
System.out.println(" obj: " + obj.a +"..."+obj.b);
B obj1 = new B();
System.out.println(" obj1: " + obj1.a +"..."+obj1.b);
Object o = obj.clone();
B obj2 = (B)o; // DownCasting
System.out.println(" obj2: " + obj2.a +"..."+obj2.b);
System.out.println("\n\n obj: "+obj.hashCode());
System.out.println(" obj1: "+obj1.hashCode());
System.out.println(" obj2: "+obj2.hashCode());
}
}
Output:
Shallow Clonning
class Address{
String cityName = "Hyderabad";
String stateName = "Telangana";
}
class Student implements Cloneable{
int sid = 101;
String sname = "ram";
int sage = 29;
Address addr = new Address();
public static void main( String args[] )
throws CloneNotSupportedException{
Student s1 = new Student();
System.out.println( s1.sid );
System.out.println( s1.sname );
System.out.println( s1.sage );
System.out.println( s1.addr.cityName );
System.out.println( s1.addr.stateName );
s1.sid = 111;
s1.sname = "Atharva";
s1.sage = 30;
s1.addr.cityName = "Dhule";
s1.addr.stateName = "Maharashtra";
System.out.println("---------S1 Details-----------");
System.out.println( s1.sid );
System.out.println( s1.sname );
System.out.println( s1.sage );
System.out.println( s1.addr.cityName );
System.out.println( s1.addr.stateName );
Student s2 = (Student)s1.clone();
System.out.println("---------S2 Details-----------");
System.out.println( s2.sid );
System.out.println( s2.sname );
System.out.println( s2.sage );
System.out.println( s2.addr.cityName );
System.out.println( s2.addr.stateName );
System.out.println("\n\n"+s1.hashCode()+" "+s2.hashCode());
// If address of anyone memory location got change then it will reflect
// to both.
s1.addr.cityName = "chennai";
System.out.println( s1.addr.cityName );
System.out.println( s2.addr.cityName );
}
}
Output:
Deep Clonning
class Address extends java.lang.Object implements Cloneable{
String cityName = "Hyderabad";
String stateName = "Telangana";
public Address clone() throws CloneNotSupportedException{
return (Address)super.clone();
}
}
class Student extends java.lang.Object implements Cloneable{
int sid = 101;
String sname = "ram";
Address addr = new Address();
public Student clone() throws CloneNotSupportedException
{
Student s3 = (Student) super.clone();
s3.addr = this.addr.clone();
return s3;
}
public static void main( String args[] )
throws CloneNotSupportedException{
Student s1 = new Student();
Student s2 = s1.clone();
System.out.println("\n\n S1: \n"+s1.sid );
System.out.println( s1.sname );
System.out.println( s1.addr.cityName );
System.out.println( s1.addr.stateName );
System.out.println("\n\n S2: \n"+s2.sid );
System.out.println( s2.sname );
System.out.println( s2.addr.cityName );
System.out.println( s2.addr.stateName );
s2.addr.cityName = " Dhule ";
s2.addr.stateName = " Maharashtra ";
System.out.println("\n\n S1: " + s1.addr.cityName );
System.out.println( s1.addr.stateName );
System.out.println("\n\n S2: " + s2.addr.cityName );
System.out.println( s2.addr.stateName );
}
}
Output: