Monday, 2 December 2013

Clonable Interface

Cloneable Interface: It is very frequency ask question doing interview and always we made a mistake to explain it.

When we are talking about marker interface and its example, so always one thing comes in our mind i.e. an interface, which has no method is called marker interface. Cloneable interface is also one of the examples of marker interface.

Clone means duplicate. In simply language we can say COPY and PASTE.

It creates new similar clone object using cloneable interface. A class implements the cloneable interface to indicate to JVM, to make copy of instance of that class. It is using Object.clone() method to make a field-for-field copy of instances of that class.

When we are using cloneable interface and invoking object's clone method on instance so it creates clone of the object and if we don't implement the cloneable interface and invoking object's clone method on instance so it throws an exception i.e. CloneNotSupportedException.

I think we should be aware that a class, where we are implementing the cloneable interface so we should override the Object.clone() method and it should be PUBLIC access specified.

Cloneable interface is available from JDK1.0.

Clone method:

This method defined under Object Class.

protected Object clone() throws CloneNotSupportedException

This method creates and return a copy of this object.

CloneNotSupportedException

This exception indicates that the clone method in class XYZ has been called, to do a process to clone an object of this class, but that this class does not implement the Cloneable interface. So I think, we keep it on our mind that we are using clone object so we should implement the cloneable interface.

Key Points:
  • Need to Implement Clonable Interface.
  • Clonable Interface is marker Interface.
  • Create dublicate copy for Object.
  • Classes which clonable interface that can be cloned only
  • If you access a class which does not implement a clonable interface so it will return CloneNotSupportedException exception.
  • When we create a clone object so constructor will not be called for clone object.
Example :

public class ClonableExample implements Cloneable{
 
 ClonableExample(){
 System.out.println("ClonableExample Constructor");
 }
 
 /**
 * @param args
 */
 public static void main(String[] args)
 {
 ClonableExample clonableExample = new ClonableExample();
 System.out.println("Obj 1 "+clonableExample.hashCode());
 try {
 ClonableExample clonableExample2 = (ClonableExample)clonableExample.clone();
 System.out.println("Obj 2 "+clonableExample2.hashCode());
 } catch (CloneNotSupportedException e) {
 e.printStackTrace();
 }
 }
 
}
 Output:

ClonableExample Constructor
Obj 1 4384790
Obj 2 9634993


 Example 2:

public class ClonableExample implements Cloneable{
 int a;
 int b;
 ClonableExample(){
 System.out.println("Contructor Call");
 }
 
 /**
 * @param args
 */
 public static void main(String[] args)
 {
 ClonableExample clonableExample = new ClonableExample();
 clonableExample.a=10;
 clonableExample.b=20;
 System.out.println("Obj 1 "+clonableExample.a + " "+clonableExample.b);
 try {
 ClonableExample clonableExample2 = (ClonableExample)clonableExample.clone();
 System.out.println("Obj 2 "+clonableExample2.a + "  "+clonableExample2.b);
 } catch (CloneNotSupportedException e) {
 e.printStackTrace();
 }
 }
 
}
 Output:

Contructor Call
Obj 1 10  20
Obj 2 10  20

I am really happy if you are also sharing your idea with me. Please free feel to ask any question to me and guide me if I am wrong because I believe on below sentence.

Mistake can make perfect

Thanks

No comments:

Post a Comment