• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Cloning in java

 
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
As you said, it is in Object so there is a basic implementation. This will copy all fields for you. This is protected so you can't just try to clone() every single object, not expecting the CloneNotSupportedException.

Now I too have asked myself why Cloneable does not declare the clone() method, but the reason is quite clear: it requires the clone() method to become public. However, sometimes you want to use clone() internally only, so you want it to remain protected. If Cloneable would declare clone() that would not be allowed.

Now the Cloneable interface does have a reason: just like Serializable, it is a marker class that the JVM will use internally. Without implementing Serializable, you cannot write the object to ObjectOutputStreams. Without implementing Cloneable, you cannot clone the object.
 
Ranch Hand
Posts: 1970
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The Cloneable interface is one of the oldest parts of Java. It is not a good example of modern Java design. So, basically, if you need its facilities, you should just learn to use it. But you should not expect particularly satisfactory answers to the "why?" questions, about Cloneable.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic