Object Cloning

// By:Team // No Comment

Email this to someonePrint this pageShare on Google+4Share on Facebook12Tweet about this on TwitterShare on LinkedIn1Share on Reddit0Pin on Pinterest0Share on StumbleUpon0


The object cloning is a way to create exact copy of an object. For this purpose, clone() method of Object class is used to clone an object.

Object Cloning

The java.lang.Cloneable interface must be implemented by the class whose object clone we want to create. If we don’t implement Cloneable interface, clone() method generates CloneNotSupportedException.

The clone() method is defined in the Object class. Syntax of the clone() method is as follows:


 

Why use clone() method ?

The clone() method saves the extra processing task for creating the exact copy of an object. If we perform it by using the new keyword, it will take a lot of processing to be performed that is why we use object cloning.

Advantage of Object cloning

Less processing task.

Example of clone() method (Object cloning)

Let’s see the simple example of object cloning


 

As you can see in the above example, both reference variables have the same value. Thus, the clone() copies the values of an object to another. So 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.

Email this to someonePrint this pageShare on Google+4Share on Facebook12Tweet about this on TwitterShare on LinkedIn1Share on Reddit0Pin on Pinterest0Share on StumbleUpon0

About Team

Browse Archived Articles by Team

Related

Sorry. There are no related articles at this time.