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
How do you add an element to an arraylist in java?
What are assembly attributes?
What is treeset in java collection?
How do I enable java in safari?
From the two, which would be easier to write: synchronization code for ten threads or two threads?
What are the two parts of a conditional statement?
What are the advantages of unicode?
How do you calculate square roots?
Write a function to print Fibonacci series and Tribonacci series?
Are arrays passed by reference in java?
What is the difference between yielding and sleeping?
Does anyone still use java?
How infinite loop is declared?
What is the program development process?
Describe method overriding