Menu Bar

Drop Down MenusCSS Drop Down MenuPure CSS Dropdown Menu

Thursday, 13 October 2016

What is Cloneable Interface In Java ?

Here, we discuss what is Cloneable Interface in Java.
Interface Cloneable

All Known Subinterfaces:
AclEntry, Attribute, AttributedCharacterIterator, Attributes, CertPathBuilderResult, CertPathParameters, CertPathValidatorResult, CertSelector, CertStoreParameters, CharacterIterator, CRLSelector, Descriptor, GSSCredential, Name

Declaration of Cloneable:
public interface Cloneable

Cloneable Interface is used to make exact copy or clone of the class.
If we invoke the Object clone() method or instance that does not implements the Cloneable interface ,then it will throw exception CloneNotSupportedException.

By convention, classes that implements cloneable interface should override Objects.clone() method ,because this interface is a marker interface ,and marker interfaces are those interface which doesn't have any methods ,they just change the behavior of the class.

After Reading Cloneable Interface one Question arises in mind that, Does clone object and original object point to the same location in memory ?
The answer is no. The clone object has its own memory space where it copies the content of the original object.That’s why when we change the content of original object after cloning, the changes does not reflect in the clone object. You can see in below example.

Example Of Cloneable Interface :

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
class Employee implements Cloneable {
 private String empName;
 public String getEmpName() {
  return empName;
 }
 public void setEmpName(String empName) {
  this.empName = empName;
 }
 @Override
 protected Object clone() throws CloneNotSupportedException {
  return (Employee)super.clone();
 }
}
public class CloneableInterfaceExample {
 public static void main(String[] args) throws CloneNotSupportedException {
  Employee ob1 = new Employee();
  ob1.setEmpName("Pushkar");
 
  //CLONING OBJECT 1 INTO OBJECT 2
  Employee ob2 = (Employee)ob1.clone(); 
 
  //CALLING BOTH OBJECTS
  System.out.println("OBJECT 1 : "+ob1.getEmpName());
  System.out.println("OBJECT 2 CLONEABLE OBJECT: "+ob2.getEmpName());
 
  System.out.println("\nCalling Both Objects Again After Changing the Content of the First Object :");    
  //CHANGING VALVE OF FIRST OBJECT
  ob1.setEmpName("Alok Nath");
 
   System.out.println("OBJECT 1 : "+ob1.getEmpName());
   System.out.println("OBJECT 2 CLONEABLE OBJECT: "+ob2.getEmpName());
 }
}

Program Output :-

OBJECT 1 : Pushkar
OBJECT 2 CLONEABLE OBJECT: Pushkar

Calling Both Objects Again After Changing the Content of the First Object :
OBJECT 1 : Alok Nath
OBJECT 2 CLONEABLE OBJECT: Pushkar

   
Blog Author - Pushkar Khosla,
Software Developer by Profession with 3.0 Yrs of Experience , through this blog i'am sharing my industrial Java Knowledge to entire world. For any question or query any one can comment below or mail me at pushkar.itsitm52@gmail.com.

This blog is all about to learn Core Java ,Interview Programs and Coding tricks to polish your Java Knowledge. If you like the content of this blog please share this with your friends.


Share this Blog with yours Friends !!

No comments:

Post a Comment