wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Cloning in java Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Cloning in java" Watch "Cloning in java" New topic
Author

Cloning in java

Ritu Kapoor
Ranch Hand

Joined: Oct 03, 2004
Posts: 102
We have a clone() defined in Object class. If we try to execute a class overriding clone(), it throws CloneNotSupportedException. But if we implement Cloneable interface(which is a marker interface), everything goes fine. Now the question is, why do not we have a clone() in Cloneable interface rather then in Object class.

Is it in the Object only because we require a default implementation of a clone() and since Object is the superclass, so we cannot somehow implement that interface in Object class. But then Why do we require a Cloneable Interface at all. We can override its implementation in our subclass also.

Kindly help me to clarify my doubts.

Regards,
Ritu
Burkhard Hassel
Ranch Hand

Joined: Aug 25, 2006
Posts: 1274
The Cloneable interface should be implemented, so when asked if an object of this class is an instanceof Cloneable you get a true.

By the way, Bert Bates somewhere here on this forum wrote that clone() is not a part of the exam.



Yours,
Bu.


all events occur in real time
Mladen Grabowsky
Greenhorn

Joined: Sep 19, 2007
Posts: 29
clone() should not be used to copy objects, use copy constructors or factory methods instead.
 
jQuery in Action, 2nd edition
 
subject: Cloning in java
 
Similar Threads
Cloning in java
Why is clone() method protected?
problem in clone...
Why is clone method present in Object if I have to implement Cloneable interface to use it?
what is the use of Clonable interface?