Simple words:
Default implementation of Object.clone() (btw you have to be clonable), provides a shallow copy,
i.e. the only primitive data type's of object will be copied but if object contains, some reference to another object, then the newly copied object will just copy the reference value of that Object.
Like
Class Employee {
int name;// this will be copied to a newly created object and set the value
int id; // this will be copied
IDepartment depart; // the newly created object will just copy the reference of the department object
//So of old object, which we are cloning/copying, changes the department object's values, the shallow copied object will also change, hence this is not 100% a freash independent copy.
}
Deep copy:
The only difference is, you are not allowed to write all the complex logic, of implementing/overriding the clone() method behaviour
Class Employee {
Default implementation of Object.clone() (btw you have to be clonable), provides a shallow copy,
i.e. the only primitive data type's of object will be copied but if object contains, some reference to another object, then the newly copied object will just copy the reference value of that Object.
Like
Class Employee {
int name;// this will be copied to a newly created object and set the value
int id; // this will be copied
IDepartment depart; // the newly created object will just copy the reference of the department object
//So of old object, which we are cloning/copying, changes the department object's values, the shallow copied object will also change, hence this is not 100% a freash independent copy.
}
Deep copy:
The only difference is, you are not allowed to write all the complex logic, of implementing/overriding the clone() method behaviour
Class Employee {
public Object clone() {
e = new employee();
Idepartment dep = new Department()
dep.name = this.department.getName()
more complex and nightmare code, of department also contains some other reference object and so on.
}
so this is deep copy but implementation is complex, to make it easy, we need to follow serialization, forcing all the participant to implement serialization, like Employee, Department,and its objects.
then just serialize and de-serialize the object and you will have two independent object with exactly same values.
again, of one of participant object doesn't implement serialize, its value will not be saved and result as null.(Confirm again)
No comments:
Post a Comment