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

What is data type example?

554


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

1607


What is the purpose of garbage collection in java, and when is it used?

566


What are the restrictions imposed on method overriding?

556


Write a program to print fibonacci series up to count 10.

520






What is anti pattern in programming?

501


Is string is a class in java?

525


How do I get the | symbol on my keyboard?

595


What is meant by nested loop?

544


Is a class subclass of itself?

605


What are the differences between c++ and java?

585


What are constructors in java?

574


What is the multi-catch block in java?

537


What does flag mean in java?

544


Explain about abstract classes in java?

588