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
Can we create an object of static class in java?
Can array grow dynamically in java?
describe method overloading
What is currentthread()?
What is a conditional equation?
Explain notify() method of object class ?
Explain restrictions for using anonymous inner classes?
What is the size of arraylist in java?
Can subclass overriding method declare an exception if parent class method doesn't throw an exception?
What is method reference?
Can arraylist contain null values?
Write a program to find the greatest of three numbers in java?
Does java initialize arrays to zero?
Can we access the non-final local variable, inside the local inner class?
Does google use java?