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
Difference between ‘>>’ and ‘>>>’ operators in java?
Is java a pure object oriented language?
What interface is extended by awt event listeners?
Difference between linkedlist and arraylist.
what is comparable and comparator interface?
Explain aggregation in java?
Does java set allow duplicates?
Explain about class in java?
Why string is called as immutable?
What are the application of stack?
What is the static keyword?
What is cr keyboard?
Can we nested try statements in java?
What things should be kept in mind while creating your own exceptions in java?
What does jenkins do?