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:

All IT Courses 50% Off
  • 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

7 Comments

  1. 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.

    Advantage of Object cloning:
    *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:
    * 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.
    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.
    *Object. Clone() will be protected, so that we have to provide our own clone() and indirectly call Object.clone() from it
    *Object. Clone() does not invoke any kind of constructor so that we don’t have any control over object construction
    *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.

  2. 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.

    Advantage of Object cloning:
    *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:
    * 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.
    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.
    *Object. Clone() will be protected, so that we have to provide our own clone() and indirectly call Object.clone() from it
    *Object. Clone() does not invoke any kind of constructor so that we don’t have any control over object construction
    *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.

  3. 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.
    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.
    *Using Clone () we can copy an array in fastest way
    Disadvantages:
    *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.
    *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.
    *Object.clone() will be protected, so that we have to provide our own clone() and indirectly call Object.clone() from it
    *Object.clone() does not invoke any kind of constructor so that we don’t have any control over object construction

  4. What is Object cloning? Explain?

    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.

    What are the advantages and disadvantages of object cloning?

    Advantage of Object cloning are:

    1 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.

    2. 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.

    3. Using Clone () we can copy an array in fastest way

    Disadvantages of cloning are:
    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.

    2. 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.
    3. Object.clone() will be protected, so that we have to provide our own clone() and indirectly call Object.clone() from it
    4. Object.clone() does not invoke any kind of constructor so that we don’t have any control over object construction

  5. 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
    Advantage of Object cloning:
    1. No need to write the repeated code .Just by writing clone()with 4 -5 lines code with abstract class.
    2. It is easy to copy when the application is already developed or old project.
    3. We can copy array in fastest way by using clone().
    Disadvantages:
    1. Object.clone() method, we need to change a lot of syntaxes in our code.
    2. 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.
    3. Object.clone() will be protected, so that we have to provide our own clone() and indirectly call Object.clone() from it
    4. 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.

  6. 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:
    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.
    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.
    Object.clone() will be protected, so that we have to provide our own clone() and indirectly call Object.clone() from it
    Object.clone() does not invoke any kind of constructor so that we don’t have any control over object construction

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

    Advantages of Object cloning:
    1. We don’t need to write lengthy and repetitive codes. We Just use an abstract class with a 4 to 5 lines long clone () method.
    2. It is the easiest and also the most efficient way of copying objects, especially if we are applying it to an already developed or an old project.
    3. Using Clone () we can copy an array quickly.

    Disadvantages of Object cloning::
    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.
    2.Object.clone() will be protected, so that we have to provide our own clone() and indirectly call Object.clone() from it.
    3.Object.clone() does not invoke any kind of constructor so that we don’t have any control over object construction.

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