File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Clone Method 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 » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Clone Method" Watch "Clone Method" New topic
Author

Clone Method

Akash Singhal
Greenhorn

Joined: Feb 12, 2005
Posts: 11
Hi,
The following code :-



gives
clone has protected access in java.lang.object inspiteof all Objects extending from java.lang.Object. Can anyone shed light on this???


P.S : I passed SCJP 1.4 today with 95 % (58/61)

Thankfully this wasnt the question i faced

Regards,
Nathaniel Stoddard
Ranch Hand

Joined: May 29, 2003
Posts: 1258
According to the API Javadocs, classes that want to be "cloneable" should override the clone method making it public, and declare to class to implement Cloneable.

Remember that when overriding a method, you cannot make it more restrictive (e.g. override a public method making it private). However, you can override a method making it more visible (e.g. override a protected clone method making it public).

I think the motivation is just to make it so that not ever single Object out there has clone() visible to other classes. Cosmetic, at best, but that's the way it is.


Nathaniel Stodard<br />SCJP, SCJD, SCWCD, SCBCD, SCDJWS, ICAD, ICSD, ICED
Tony Morris
Ranch Hand

Joined: Sep 24, 2003
Posts: 1608
Cloning in Java is extremely broken.
In fact, it is the second most broken part of the core API in my opinion, and there are *lots* of broken parts. That is, there is a general consensus that cloning is very much broken, but I take it further to say it's more broken that most things. In any case, the use of cloning (even when implemented correctly) is only justified in a few obscure cases, and it is more abused than not (how about that hey? It's extremely broken, yet people insist on misusing an API that offers so little than it is less than zero).

Part of this brokenness is the declaration of the Object.clone() method as protected. A Cloneable class is supposed to override the method with public access (yes, one of the cloning design flaws, but that's the intention of the API designers). So if you call it on a type that is not supposed to be cloned, you will receive a compile-time error. java.lang.String is one of these classes (it is not intended to be cloned).

In case you're wondering, the worst part of the API is that System.in/err/out are declared final while exposing setIn/Err/Out methods. <quiz>So how is it implemented?</quiz>


Tony Morris
Java Q&A (FAQ, Trivia)
Robert Hayes
Ranch Hand

Joined: Oct 24, 2004
Posts: 116
See the following link for an interesting read on cloning:

http://www.agiledeveloper.com/articles/cloning072002.htm
Stephen C Johnson
Greenhorn

Joined: Mar 07, 2005
Posts: 7
An interesting alternative to cloning an object is copying through serialization.

You can create a copy by using Object Streams.

We use this as a "poor man's clone", but it works better than the provided clone method/approach.

Steve
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Clone Method