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 many digits is int32?
How do you generate random numbers in java?
What is super in java?
What is Mutex (Mutual Exclusion Object) ?
Is an empty arraylist null?
What do you mean by composition in java?
Why singleton is not thread safe?
Is object a data type in java?
what is abstract class in Java?
What is lambda expression in java?
Is oracle charging for java?
What happens when main () method is declared as private?
What are the drawbacks for singleton class?
What is jit and its use?
What is difference between Heap and Stack Memory?