Thursday, April 6, 2017

cloning in java with example

In this article we will learn about clone() method which is a tricky method from java.lang.Object class. The process of creating an exact copy of an existing object is called "cloning". The main objective of cloning is to maintain backup i.e. if something goes wrong we can recover the situation by using backup copy.For this purpose,clone() method of Object class is used to clone the object.All the Objects can’t have the capability to produce cloned Objects. Only clonaeble objects having that capability.An Object is said to be cloneable iff the corresponding class has to implement java.lang.cloneable interface.It doesn’t contain any methods it is a marker interface. 

There are two types of cloning. They are

  1. Shallow cloning
  2. Deep cloning
1.Shallow cloning:  The process of creating just duplicate reference variable but not duplicate object is called shallow cloning.

Example:
Test t1=new Test();
Test t2=t1;










2. Deep cloning:  The process of creating exactly independent duplicate object is called deep cloning.

Example:
Test t1=new Test();
Test t2=(Test)t1.clone();
System.out.println(t1==t2)//false
System.out.println(t1.hashcode()==t2.hashCode());//false










Note: cloning by default deep cloning()

The following steps to clone the object of a class:


  • An object is said to be Cloneable if and only if the corresponding class implements cloneable interface.
  • The cloneable interface belongs to java.lang package and it does not contain any methods since it is marker interface where the required ability will be provided automatically by JVM.
  • clone() method of Object class is used for cloning.Object is the super class for every class.
  • If Cloneable interface is not implemented,then that class objects can not be cloned and it give CloneNotSupportedException
Example:
class Test implements Cloneable
{
int a=10;
int b=20;
public static void main(String args[])throws CloneNotSupportedException
{
Test t1=new Test();
Test t2=(Test)t1.clone();
t2.a=555;                                                                  
t2.b=888;
System.out.println(t1.a+"-------"+t1.b);
}
}








Output:




It is also possible to create a cloned object,by overriding the clone() method of Object class.

The following program to make cloning Student class object by writing our own myClone() method,from where Object class clone() method is called.

class Student implements Cloneable
{
int id;

String name;
//constructor to initialize variables
Student(int id,String name)
{
this.id=id;
this.name=name;
}
//methods to display the details
void getData()
{
System.out.println("Id="+id);
System.out.println("Name="+name);
}
//clone the present class object
public Object myClone()throws CloneNotSupportedException
{
return super.clone();

}
}

class CloneDemo
{
public static void main(String args[])throws 
CloneNotSupportedException
{
//create student class object using new operator
Student s1=new Student(10,"lucky");
System.out.println("Original object");
s1.getData();
//create another object by cloning s1 as myClone() method returns object of Object class type and it should convert into student type
Student s2=(Student)s1.myClone();
System.out.println("Cloned object");
s2.getData();
}
}
Output:









ALOS READ:
When to use abstract class and interface in Java
Why String is immutable in java
Java HashMap with example
Top 10 Java Programmer Mistakes
How to find duplicate elements in Array


No comments:

Post a Comment

High Paying Jobs after Learning Python

Everyone knows Python is one of the most demand Programming Language. It is a computer programming language to build web applications and sc...