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 data type example?
Hi all, I am dng a mini project on FileSplitter application which splits the GBs of logfile into Smaller chunks(mbs) depending on the split size." How to handle GBs file? I am getting OutOfMemoryException, when I input such GB sized file. Thx
What is the purpose of garbage collection in java, and when is it used?
What are the restrictions imposed on method overriding?
Write a program to print fibonacci series up to count 10.
What is anti pattern in programming?
Is string is a class in java?
How do I get the | symbol on my keyboard?
What is meant by nested loop?
Is a class subclass of itself?
What are the differences between c++ and java?
What are constructors in java?
What is the multi-catch block in java?
What does flag mean in java?
Explain about abstract classes in java?