Cloning Method In Java

Hello guys!! Today we are going to learn cloning method in java.

Cloning Method

Cloning method is one of the method of object class. Object cloning is nothing but creating a copy or duplicating an object using clone method.

For example:

Cloning Method In Java

 

Here you can see “employeeDetailsObj” object. It has two fields Name and EmpID.

Name is George and EmpID is 458464. Using clone method we can create “clonedEmployeeDetailsObj” object and which contains same fields and same values.

Also Read – Best Way To Copy Array In Java

There is no operator in java for copying an object because objects are manipulated through reference variables.

Assignment operator can be used to a create copy but it only duplicates reference not the object. Clone method can be used here.

how to implement cloning

If a class or one of its superclasses implements cloneable interface you can use the clone method to create a copy from existing object.

If the object does not implement cloneable interface then if you call clone method of that particular object it will throw CloneNotSupportedException.

This is the way we can declare clone method,

public Object clone() throws CloneNotSupportedException

or

protected Object clone() throws CloneNotSupportedException

The simplest way to make your class cloneable is to add implements cloneable to your class declaration.

Then your objects can invoke the clone method.

If the object on which clone method was invoked implement the cloneable interface, Object’s implementation of the clone method creates an object of the same class as the original object.

And initializes the new object’s member variables to have the same values as the original object’s corresponding member variables.

types of cloning

There are two types of cloning:

  • Shallow Cloning – is a default behavior of cloning. It’s copying reference pointer to object.

That is, new object is pointing to the same memory reference of the old             object.

In shallow cloning memory usage is low since it creates a new instance              of the same class and copies all the fields to the new instance and                      returns it.

  • Deep Cloning – is copy of object itself. When the copied object contains some other object it references are cloned recursively in deep cloning.

Please note that when you implement deep cloning be careful of cyclic              dependencies.

example: cloning method in java

public class Employee implements Cloneable
{

private String empName;
private String empID;

public Employee(String empName, String empID)
{

super();
this.empName = empName;
this.empID = empID;

}

public String getEmpName()
{
return empName;
}

public void setEmpName(String empName)
{
this.empName = empName;
}

public String getEmpID()
{
return empID;
}

public void setEmpID(String empID)
{
this.empID = empID;
}

@Override
public Object clone() throws CloneNotSupportedException
{
return super.clone();
}
}

Here you can see Employee class which implements Cloneable interface. It has two fields empName and empID.

It has Employee constructor. Using this constructor we can create employee object and it has getter and setter methods for empName and empID.

Lastly I have overridden clone method (as you can see above).

public class CloneDemo
{
public static void main(String[] args)throws CloneNotSupportedException
{

Employee empObj = new Employee("George", "458464"); System.out.println("Original Employee Details : "); System.out.println("Employee Name - " + empObj.getEmpName()); System.out.println("Employee ID - " + empObj.getEmpID());

Employee clonedEmpObj = (Employee) empObj.clone();

System.out.println("----------------------------------"); System.out.println("Cloned Employee Details : "); System.out.println("Employee Name - " + clonedEmpObj.getEmpName());
System.out.println("Employee ID - " + clonedEmpObj.getEmpID());

}
}

As you can see above, I have defined CloneDemo. It has a main method.

First let’s see the output of above example,

Cloning Method In Java

As you can see in the output, I have created “empObj” object and displaying employee name and ID number.

Name is “George” and ID is 458464. Using clone method I am cloning “empObj” object and creating “clonedEmpObj” object.

Then I am displaying empName and empID of “clonedEmpObj” object. If you notice “Original Employee Details” name and ID is equal to “Cloned Employee Details” name and ID.

conclusion:

So that’s about cloning method in java. I hope you have understood the concept. You can subscribe to my blog if haven’t.

About the author

admin

View all posts

Leave a Reply

Your email address will not be published. Required fields are marked *