Cloning basically refers to creating an identical copy of an existing object.
The term clone is derived from κλών, the Greek word for "twig, branch", referring to the process whereby a new plant can be created from a twig.
The word 'Cloning' in java refers to the same meaning. When we say we have created a clone object in java we mean we have created a new object with identical values in the object.
There are two type of cloning in java as below :
1. Shallow cloning
2. Deep cloning
Since this article is about shallow cloning will give description about that only and will include about the deep cloning in my next post.
Shallow cloning :
So shallow cloning is nothing but creating a clone of the object but not in detail. Now what does that mean?
It means that if you have a object with referenced object in your main object then shallow cloning will not clone them, it will just clone the attribute of the main object.
Let's take an example :
I have a class
Address class
Now shallow cloning can be done easily by using cloneable interface.
Example :
class person implements Cloneable
{
Long identifier;
String firstName;
String lastName;
Address address;
// remaining code goes here
public Object clone() throws CloneNotSupportedException
{
super.clone();
}
}
Now if I call this method from my main method then you will get a shallow clone of the object person.
That means you will get a new object of class Person but this is still pointing to old address object. It will just copy the reference of that object.
The term clone is derived from κλών, the Greek word for "twig, branch", referring to the process whereby a new plant can be created from a twig.
The word 'Cloning' in java refers to the same meaning. When we say we have created a clone object in java we mean we have created a new object with identical values in the object.
There are two type of cloning in java as below :
1. Shallow cloning
2. Deep cloning
Since this article is about shallow cloning will give description about that only and will include about the deep cloning in my next post.
Shallow cloning :
So shallow cloning is nothing but creating a clone of the object but not in detail. Now what does that mean?
It means that if you have a object with referenced object in your main object then shallow cloning will not clone them, it will just clone the attribute of the main object.
Let's take an example :
I have a class
class person
{
Long identifier;
String firstName;
String lastName;
Address address;
// remaining code goes here
}
Address class
class Address
{
Long identifier;
String streetName;String state;
// remaining code goes here}
Now shallow cloning can be done easily by using cloneable interface.
Example :
class person implements Cloneable
{
Long identifier;
String firstName;
String lastName;
Address address;
// remaining code goes here
public Object clone() throws CloneNotSupportedException
{
super.clone();
}
}
Now if I call this method from my main method then you will get a shallow clone of the object person.
That means you will get a new object of class Person but this is still pointing to old address object. It will just copy the reference of that object.
Comments
Post a Comment
.