File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Small doubt in Object Clone

 
Puja Agarwal
Greenhorn
Posts: 11
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi all,
I have a small doubt in cloning of objects

When should i implement Cloneable interface? As per my book i should when i want redefine clone to be public to allow objects to be cloned by any method.

But if i am not wrong since clone() is in Object class and is protected i can always override it and can increase it visibility to public then why do i need a marker interface at all


Thanks in advance
Puja
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24204
34
Chrome Eclipse IDE Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The marker interface is used by Object's implementation of clone() itself! Conceptually, at least, it looks like



So you see that implementing Cloneable is a signal to this method that it's OK to make a copy.
 
Puja Agarwal
Greenhorn
Posts: 11
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Ernest , but could you please elaborate a little more , when i try to view the code of java.lang.Object class it shows clone method as


protected native Object clone() throws CloneNotSupportedException;


it is becoming more difficult for me know , why does this method does not have a body and if it can not have a body then it should be declared abstract


Thanks in advance
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24204
34
Chrome Eclipse IDE Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A native method is one whose body is actually written in another language, like "C". I said that my Java code was conceptually what clone() looks like -- in other words, it behaves as if that's what the code looks like.
 
Puja Agarwal
Greenhorn
Posts: 11
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks finally i got it .
 
Don't get me started about those stupid light bulbs.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic