Cloning is a process of creating an exact copy of an
existing object in the memory.
In java,clone() method of java.lang.Object class is used for cloning process. This method creates an exact copy of an object on which it is called through field-by-field assignment and returns the reference of that object.
Not all the objects in java are eligible for cloning process. The objects which implement Cloneable interface are only eligible for cloning process. Cloneable interface is a marker interface which is used to provide the marker to cloning process.
The first and foremost way is already mentioned in the above github link. There are a couple of drawbacks of using this approach
If you dont want to go this route then there are two ways on you can create a Deep Copy
In java,clone() method of java.lang.Object class is used for cloning process. This method creates an exact copy of an object on which it is called through field-by-field assignment and returns the reference of that object.
Not all the objects in java are eligible for cloning process. The objects which implement Cloneable interface are only eligible for cloning process. Cloneable interface is a marker interface which is used to provide the marker to cloning process.
Shallow Copy vs Deep Copy
It’s very simple
that if the object has only primitive fields or Immutable fields, then
obviously you will go for shallow copy but if the object has references to
other objects, then based on the requirement shallow copy or deep copy should
be chosen. What I mean here is, if the references are not modified anytime, and
then there is no point in going for deep copy. You can just opt shallow copy.
But if the references are modified often, then you need to go for deep copy.
Again there is no hard and fast rule; it all depends on the requirement.
Shallow Copy
Whenever
we use default implementation of clone method we get shallow copy of object
means it create new instance and copy all the field of object to that new
instance and return it as object
type we need to explicitly
cast it back to our original object. This is shallow copy of the object. clone() method of the object class
support shallow copy of the object. If the object contains primitive as well as non-primitive or
reference type variable In shallow
copy, the cloned object also refers to the same object to which the original
object refers as only the object references gets copied and not the referred
objects themselves. That's why the name shallow copy or shallow cloning in
Java. If only primitive type fields or Immutable objects are there then there is no
difference between shallow and deep copy in Java.
https://github.com/omerhashmininjago/java-programs/tree/master/src/main/java/com/demonstrate/concepts/cloneable/example1
Deep Copy
Whenever we need our own meaning of a copy and don't use default implementation
we call it as deep copy, whenever we need deep copy of the object we need to
implement it according to our need. So for deep copy we need to ensure all the
member class also implement the Cloneable interface and override the clone() method of the object class. After that we override the clone() method in all those classes even in the classes where we have only primitive type members otherwise we would not be
able to call the protected clone() method of Object class on the instances of those classes inside some other class. It’s
typical restriction of the protected access.
https://github.com/omerhashmininjago/java-programs/tree/master/src/main/java/com/demonstrate/concepts/cloneable/example2
How to implement Deep Copy?
The first and foremost way is already mentioned in the above github link. There are a couple of drawbacks of using this approach
- Every Reference class needs to implement Cloneable and override the clone method
- What if the class has final fields (they are only initialized through Constructor) and clone does not call the constructor
- If a class is implementing Clonebale, then all its superclasses also need to define the clone method or inherit it form their parents, else super.clone() will fail
If you dont want to go this route then there are two ways on you can create a Deep Copy
- Creating your own implementation
- Using an external Library
Creating your own Implementation
- Serialization
We can serialize the object and then deserialize it, this would give us the copy of the original object, but this also has drawbacks - still need to implement Serializable
- Wont be able to modify final fields
- Serialization process is slower than cloning
https://github.com/omerhashmininjago/java-programs/tree/master/src/main/java/com/demonstrate/concepts/cloneable/example4 - Copy Constuctor
This overcomes every design issue that comes with calling the clone method.Here we dont even need to implement any interface
https://github.com/omerhashmininjago/java-programs/tree/master/src/main/java/com/demonstrate/concepts/cloneable/example3
Using an external Library
We can also opt for external libraries like below
- BeanUtils.cloneBean(object) - this creates a shallow clone similar to Object.clone()
- SerializationUtils.clone(object) - this created a deep clone; but all classes need to implement Serializable
https://github.com/omerhashmininjago/java-programs/tree/master/src/main/java/com/demonstrate/concepts/cloneable/example5
No comments:
Post a Comment