What is cloneable interface?

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


Please Help Members By Posting Answers For Below Questions

Can we create an object of static class in java?

789


Can array grow dynamically in java?

696


describe method overloading

704


What is currentthread()?

755


What is a conditional equation?

763


Explain notify() method of object class ?

841


Explain restrictions for using anonymous inner classes?

817


What is the size of arraylist in java?

770


Can subclass overriding method declare an exception if parent class method doesn't throw an exception?

735


What is method reference?

703


Can arraylist contain null values?

742


Write a program to find the greatest of three numbers in java?

755


Does java initialize arrays to zero?

697


Can we access the non-final local variable, inside the local inner class?

752


Does google use java?

734