Answer Posted / radhika
Java uses clone() of Object class to copy content of 1
object to other object. Classes can implement Cloneable
Interface to override clone() method of object class.
The following sample code will show the procedure for
implementing cloneable interface.
public class CloneExp implements Cloneable {
private String name;
private String address;
private int age;
private Department depart;
public CloneExp(){
}
public CloneExp(String aName, int aAge, Department aDepart) {
this.name = aName;
this.age = aAge;
this.depart = aDepart;
}
protected Object clone() throws CloneNotSupportedException {
CloneExp clone=(CloneExp)super.clone();
// make the shallow copy of the object of type Department
clone.depart=(Department)depart.clone();
return clone;
}
public static void main(String[] args) {
CloneExp ce=new CloneExp();
try {
// make deep copy of the object of type CloneExp
CloneExp cloned=(CloneExp)ce.clone();
} catch (CloneNotSupportedException e) {
e.printStackTrace();
}
}
}
| Is This Answer Correct ? | 3 Yes | 3 No |
Post New Answer View All Answers
What is the difference between variable & constant?
How arrays are stored in memory in java?
When is an object subject to garbage collection?
Convert Binary tree to linked list.
List types of storage classes in java?
How do you declare a variable?
What is the program development process?
What is the difference between multiple processes and multiple threads?
What is the exception hierarchy in java?
What is the significance of listiterator?
What are untrusted applets?
What are different types of classloaders?
How many bytes is a unicode character?
What is the use of conditional statement?
When a lot of changes are required in data, which one should be a preference to be used? String or stringbuffer?