aspose file tools*
The moose likes Beginning Java and the fly likes Small doubt in Object Clone Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Small doubt in Object Clone" Watch "Small doubt in Object Clone" New topic
Author

Small doubt in Object Clone

Puja Agarwal
Greenhorn

Joined: May 29, 2008
Posts: 11
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

Joined: Jul 08, 2003
Posts: 24187
    
  34

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.


[Jess in Action][AskingGoodQuestions]
Puja Agarwal
Greenhorn

Joined: May 29, 2008
Posts: 11
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

Joined: Jul 08, 2003
Posts: 24187
    
  34

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

Joined: May 29, 2008
Posts: 11
Thanks finally i got it .
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Small doubt in Object Clone