• 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

Why is clone method present in Object if I have to implement Cloneable interface to use it?

 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi everyone,
The clone() method declared in Object class raises CloneNotSupportedException, if the class does not implement Cloneable interface. Then, why is the method present in Object class? Is it just to give a default implementation for clone method? Since Cloneable is an interface, it cannot have default implementation. If Cloneable is declared as a class, then it should be extended. But , Java does not support multiple inheritance. Am I right?
Still I wonder , how the JVM make sure that the calling class implements a Cloneable interface when it is trying to clone method? Is this check implemented in the clone method definition?
 
Saloon Keeper
Posts: 15485
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Object's clone() method probably just checks it using: this instanceof Cloneable. The reason that the clone() method is defined in the Object class, is because some 'magic' is needed to actually make a clone. First of all, a new object has to be created without the use of a constructor. Secondly, the object has to be of the same type as the original object, even though the original may only know its exact type at runtime. The designers probably felt it was easiest to put all this magic in the Object class, since it is a bit special to begin with.

The real question one should ask is, why is there a clone() method at all? Classes that lend themselves to having their instances cloned should have a copy constructor. Copy constructors are much easier to implement correctly, and don't need any magic to work.
 
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Kalpana Periasamy wrote:Then, why is the method present in Object class?



Probably so we can always just call super.clone() and it will work.

Also, note that clone() and Cloneable have been around since the very beginning, or nearly so. A lot of decisions were made in the early days of Java where the designers didn't have the complete picture yet and didn't know where Java's path would lead. or else they simply had to go with what was the most expedient at the time in order to meet a release date.

Whenever you see something that looks a little bit weird, check to see if it's been around for a long time, and be willing to consider it in the context of the Java landscape 15-18 years ago.
 
But how did the elephant get like that? What did you do? I think all we can do now is read this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic