In Shallow copy all the fields are just copied from source object to destination object. If the source object is containing any field that holds a reference to another object then only the reference will be copied i.e. source and destination objects field(that particular field) will point to same memory location. Java code for Shallow Copy package shallow; class JobDescription { private String designation; public JobDescription(String designation) { this.designation = designation; } public String getDesignation() { return designation; } public void setDesignation(String designation) { this.designation = designation; } @Override public String toString() {...
In Deep copy all the fields are deeply copied from source object to destination object. If the source object is containing any field that holds a reference to another object then for the destination object a new copy will be created i.e. source and destination objects field will point to different memory location. Java code for Deep Copy package deep; class JobDescription { private String designation; public JobDescription(String designation) { this.designation = designation; } public String getDesignation() { return designation; } public void setDesignation(String designation) { this.designation = designation; } @Override public String toString() { ...
Cloneable interface is a marker interface which used for marking a class so that its objects can be cloned. If a class is not marked as cloneable and we try to clone its objects then "CloneNotSupportedException" will be thrown. Types of cloning in java: Shallow Cloning (Shallow Copy) In Shallow copy all the fields are just copied from source object to destination object. If the source object is containing any field that holds a reference to another object then only the reference will be copied i.e. source and destination objects field(that particular field) will point to same memory location. Deep Cloning (Deep copy) In Deep copy all the fields are deeply copied from source object to destination object. If the source object is containing any field that holds a reference to another object then for the destination object a new copy will be created i.e. source and destination objects field will point to different memory location.
Comments
Post a Comment