• 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
  • Tim Cooke
  • paul wheaton
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

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?
 
Bartender
Posts: 15737
368
  • 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.
 
I'd appreciate it if you pronounced my name correctly. Pinhead, with a silent "H". Petite ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic