what is cloning in java ,cloning is making a exact copy of an object. in java cloning is done by field by field of primitive data types and return reference to objects
to clone a class we have to implement Cloneable interface
override the method public object clone
package blogging;
public class Shallowcloning implements Cloneable {
private int age;
private int salary;
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public int getSalary() {
return salary;
}
public void setSalary(int salary) {
this.salary = salary;
}
public Shallowcloning() {
}
public Object clone() throws CloneNotSupportedException{
return super.clone();
}
}
package blogging;
public class Clonningimplementation {
public static void main(String[] args) {
try{
Shallowcloning sclone=new Shallowcloning();
sclone.setSalary(200);
sclone.setAge(28);// setting value for the objects
//creting a new clone for object sclone
Shallowcloning sclone2=(Shallowcloning)sclone.clone();
//checking the values of clone 1 and clone 2
System.out.println("Sclone salary:"+sclone.getSalary());
System.out.println("Sclone1 Age:"+sclone.getAge());
System.out.println("Sclone2 salary:"+sclone2.getAge());
System.out.println("Sclone2 Age:"+sclone2.getSalary());
}
catch(CloneNotSupportedException e){
System.out.println("some problem occours with clonning ");
}
}
}
output
Sclone salary:200
Sclone1 Age:28
Sclone2 salary:28
Sclone2 Age:200
what happened here is a field by field copy of object 1 ,the point to be noted here is object 2 is independent of object 1 ,so any changes done in object 2 will not affect object 1(only if primitive data types are present if reference data types are present we have to go for deep cloning )
sclone2.setAge(67);
sclone2.setSalary(78000);//changing clone objects methods
//after changing
Sclone2 salary:200
Sclone salary:200
Sclone1 Age:28
Sclone2 age:67
Sclone2 salary:78000
see the changes made to object 2 i.e the cloned object didn't affect the original object
till now we have seen shallow cloning ,next we shall see what is deep cloning
Deep cloning
so far we have cloned only primitive data types what if the object contains refrences for eg
package blogging;
public class Deepcloning implements Cloneable
{
private Shallowcloning shallowcopy;
public Deepcloning() {
}
public Shallowcloning getShallowcopy() {
return shallowcopy;
}
public void setShallowcopy(Shallowcloning shallowcopy) {
this.shallowcopy = shallowcopy;
}
public String getEmployee() {
return employee;
}
public void setEmployee(String employee) {
this.employee = employee;
}
private String employee;
public Object clone() throws CloneNotSupportedException{
return super.clone();}
}
here the class Deepcloning has a reference
public class Deepcloning implements Cloneable
{
public Shallowcloning shallowcopy;
public Deepcloning() {
}
public Shallowcloning getShallowcopy() {
return shallowcopy;
}
public void setShallowcopy(Shallowcloning shallowcopy) {
this.shallowcopy = shallowcopy;
}
public String getEmployee() {
return employee;
}
public void setEmployee(String employee) {
this.employee = employee;
}
private String employee;
public Object clone() throws CloneNotSupportedException{
Deepcloning shls=(Deepcloning)super.clone();
shls.setShallowcopy((Shallowcloning)shallowcopy.clone());
return shls;
}
}
and the implementation method
Deepcloning dp1=new Deepcloning();
dp1.setEmployee("solomon");
dp1.setShallowcopy(new Shallowcloning());
dp1.getShallowcopy().setAge(11);
dp1.getShallowcopy().setSalary(3000);
System.out.println("orignal copy employee"+dp1.getEmployee());
System.out.println("orignal copy employee"+dp1.getShallowcopy().getAge());
System.out.println("orignal copy employee"+dp1.getShallowcopy().getSalary());
Deepcloning dp2=(Deepcloning)dp1.clone();
System.out.println("clone copy employee"+dp2.getEmployee());
System.out.println("clone copy employee"+dp2.getShallowcopy().getAge());
System.out.println("clone copy employee"+dp2.getShallowcopy().getSalary());
dp1.getShallowcopy().setAge(25);
System.out.println("original value"+dp1.getShallowcopy().getAge());
System.out.println("clonned value "+dp2.getShallowcopy().getAge());
to clone a class we have to implement Cloneable interface
override the method public object clone
package blogging;
public class Shallowcloning implements Cloneable {
private int age;
private int salary;
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public int getSalary() {
return salary;
}
public void setSalary(int salary) {
this.salary = salary;
}
public Shallowcloning() {
}
public Object clone() throws CloneNotSupportedException{
return super.clone();
}
}
package blogging;
public class Clonningimplementation {
public static void main(String[] args) {
try{
Shallowcloning sclone=new Shallowcloning();
sclone.setSalary(200);
sclone.setAge(28);// setting value for the objects
//creting a new clone for object sclone
Shallowcloning sclone2=(Shallowcloning)sclone.clone();
//checking the values of clone 1 and clone 2
System.out.println("Sclone salary:"+sclone.getSalary());
System.out.println("Sclone1 Age:"+sclone.getAge());
System.out.println("Sclone2 salary:"+sclone2.getAge());
System.out.println("Sclone2 Age:"+sclone2.getSalary());
}
catch(CloneNotSupportedException e){
System.out.println("some problem occours with clonning ");
}
}
}
output
Sclone salary:200
Sclone1 Age:28
Sclone2 salary:28
Sclone2 Age:200
what happened here is a field by field copy of object 1 ,the point to be noted here is object 2 is independent of object 1 ,so any changes done in object 2 will not affect object 1(only if primitive data types are present if reference data types are present we have to go for deep cloning )
sclone2.setAge(67);
sclone2.setSalary(78000);//changing clone objects methods
//after changing
Sclone2 salary:200
Sclone salary:200
Sclone1 Age:28
Sclone2 age:67
Sclone2 salary:78000
see the changes made to object 2 i.e the cloned object didn't affect the original object
till now we have seen shallow cloning ,next we shall see what is deep cloning
Deep cloning
so far we have cloned only primitive data types what if the object contains refrences for eg
package blogging;
public class Deepcloning implements Cloneable
{
private Shallowcloning shallowcopy;
public Deepcloning() {
}
public Shallowcloning getShallowcopy() {
return shallowcopy;
}
public void setShallowcopy(Shallowcloning shallowcopy) {
this.shallowcopy = shallowcopy;
}
public String getEmployee() {
return employee;
}
public void setEmployee(String employee) {
this.employee = employee;
}
private String employee;
public Object clone() throws CloneNotSupportedException{
return super.clone();}
}
here the class Deepcloning has a reference
public class Deepcloning implements Cloneable
{
public Shallowcloning shallowcopy;
public Deepcloning() {
}
public Shallowcloning getShallowcopy() {
return shallowcopy;
}
public void setShallowcopy(Shallowcloning shallowcopy) {
this.shallowcopy = shallowcopy;
}
public String getEmployee() {
return employee;
}
public void setEmployee(String employee) {
this.employee = employee;
}
private String employee;
public Object clone() throws CloneNotSupportedException{
Deepcloning shls=(Deepcloning)super.clone();
shls.setShallowcopy((Shallowcloning)shallowcopy.clone());
return shls;
}
}
and the implementation method
Deepcloning dp1=new Deepcloning();
dp1.setEmployee("solomon");
dp1.setShallowcopy(new Shallowcloning());
dp1.getShallowcopy().setAge(11);
dp1.getShallowcopy().setSalary(3000);
System.out.println("orignal copy employee"+dp1.getEmployee());
System.out.println("orignal copy employee"+dp1.getShallowcopy().getAge());
System.out.println("orignal copy employee"+dp1.getShallowcopy().getSalary());
Deepcloning dp2=(Deepcloning)dp1.clone();
System.out.println("clone copy employee"+dp2.getEmployee());
System.out.println("clone copy employee"+dp2.getShallowcopy().getAge());
System.out.println("clone copy employee"+dp2.getShallowcopy().getSalary());
dp1.getShallowcopy().setAge(25);
System.out.println("original value"+dp1.getShallowcopy().getAge());
System.out.println("clonned value "+dp2.getShallowcopy().getAge());