All IT Courses 50% Off
QA Tutorials

Object Cloning in Java

Object cloning is the way to create exact copy of an object, The clone () method of Object class is used as to clone an object. The java.lang. Cloneable interface will be implemented by the class whose object is a clone we want to create. Suppose, if we don’t implement Cloneable interface, clone() method will generate CloneNotSupportedException

Syntax is:

Protected object clone () throws clonenotsupportedexception

Why use clone () method?

The clone () method will definitely save the extra processing task for creating the exact copy of an object. Suppose if we will perform it by using the new keyword, it will take a lot of processing time and also to be performed that is the reason why we use object cloning.

Advantage of Object cloning:

Although Object. Clone () has some design issues but it will be still a popular and easy way of copying objects. Following are the advantages of using clone () method:

  • we don’t need to write lengthy and also repetitive codes. We Just use an abstract class with a 4- or 5-line long clone () method.
  • It is the easiest and also the most efficient way for copying objects, especially if we are applying it to an already developed or an old project. Defining a parent class, in which Cloneable is to be implemented, and provide the definition of the  clone () method and the task will be done.
  • Using Clone () we can copy an array in fastest way

Disadvantages:

  1. Object.clone() method, we need to change a lot of syntaxes in our code which includes: implementing a Cloneable interface, defining the clone() method and handling CloneNotSupportedException, and finally, calling Object.clone() etc.
  1. We have to implement cloneable interface while it doesn’t have any methods in it. We just need to use it to tell the JVM that we can perform clone () on our object.
  1. Object.clone() will be protected, so that we have to provide our own clone() and indirectly call Object.clone() from it
  1. Object.clone() does not invoke any kind of constructor so that we don’t have any control over object construction
  1. If we want to write a clone method in a child class, then all of its superclasses will be define the clone () method in them or inherit it from another parent class. Otherwise, the super.clone() chain will fail.

Example:

Class student implements cloneable
{
Int rollno;
String name;
Student (int rollno, string name)
{
This.rollno = rollno;
 
This.Name = name;
}
 
Public object clone ()throws clonenotsupportedexception
{
Return super clone ();
}
Public static void main (string args [])
Try
{
Student s1=new student (105, “Jennette”).
Student s2=new student (290,” Jeffery”);
System.out.println(s1. rollno+ ““+ s1);
System.out.println(s2. rollno+” “+s2);
}
Catch(clonesupportedexception)
}
}
Output:
105 Jennette
105 Jennette

As we can see in the example, both reference variables will have the same value. Thus, the clone () will copy the values of an object to another. We don’t need to write explicit code to copy the value of an object to another.

If we create another object by new keyword and assign the values of another object to this one, it will require a lot of processing on this object. So to save the extra processing task we use clone () method.

Questions

  1. What is Object cloning? Explain?
  2. What are the advantages and disadvantages of object cloning?
Facebook Comments

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Related Articles

Back to top button
Close
Close

Get Python Course
worth 499$ for FREE!

Offer valid for 1st 20 seats only, Hurry up!!

You have successfully subscribed to the newsletter

There was an error while trying to send your request. Please try again.

H2kinfosys Blog will use the information you provide on this form to be in touch with you and to provide updates and marketing.