1) What are different types of cloning in Java?
Java supports two type of cloning: - Deep and shallow cloning. By default shallow copy is used in Java. Object class has a method clone() which does shallow cloning.
2) What is Shallow copy?
In shallow copy the object is copied without its contained objects. Shallow clone only copies the top level structure of the object not the lower levels. It is an exact bit copy of all the attributes.
Figure 1: Original java object obj
The shallow copy is done for obj and new object obj1 is created but contained objects of obj are not copied.
Figure 2: Shallow copy object obj1
It can be seen that no new objects are created for obj1 and it is referring to the same old contained objects. If either of the containedObj contains any other object no new reference is created.
3) What is deep copy and how it can be achieved?
In deep copy the object is copied along with the objects it refers to. Deep clone copies all the levels of the object from top to the bottom recursively.
Figure 3 : Original Object obj
When a deep copy of the object is done new references are created.
Figure 4: obj2 is deep copy of obj1
One solution is to simply implement your own custom method (e.g., deepCopy()) that returns a deep copy of an instance of one of your classes. This may be the best solution if you need a complex mixture of deep and shallow copies for different fields, but has a few significant drawbacks:
- You must be able to modify the class (i.e., have the source code) or implement a subclass. If you have a third-party class for which you do not have the source and which is marked final, you are out of luck.
- You must be able to access all of the fields of the class̢۪s superclasses. If significant parts of the object̢۪s state are contained in private fields of a superclass, you will not be able to access them.
- You must have a way to make copies of instances of all of the other kinds of objects that the object references. This is particularly problematic if the exact classes of referenced objects cannot be known until runtime.
- Custom deep copy methods are tedious to implement, easy to get wrong, and difficult to maintain. The method must be revisited any time a change is made to the class or to any of its superclasses.
4) What is difference between deep and shallow cloning?
The differences are as follows:
- Consider the class:
public class MyData{
String id;
Map myData;
}
String id;
Map myData;
}
The shallow copying of this object will have new id object and values as “” but will point to the myData of the original object. So a change in myData by either original or cloned object will be reflected in other also. But in deep copying there will be new id object and also new myData object and independent of original object but with same values.
Shallow copying is default cloning in Java which can be achieved using clone() method of Object class. For deep copying some extra logic need to be provided
5) What are the characteristics of a shallow clone?
If we do
a = clone(b);
Then,
1) b.equals(a)
2) No method of a can modify the value of b
1) b.equals(a)
2) No method of a can modify the value of b
6) What are the disadvantages of deep cloning?
Disadvantages of using Serialization to achieve deep cloning –
- Serialization is more expensive than using object.clone().
- Not all objects are serializable.
Serialization is not simple to implement for deep cloned object
No comments:
Post a Comment