The Artima Developer Community
Sponsored Link

Crippled Clone

Advertisement

Advertisement

This page contains an archived post to the Design Forum (formerly called the Flexible Java Forum) made prior to February 25, 2002. If you wish to participate in discussions, please visit the new Artima Forums.

Message:

Crippled Clone

Posted by Adrian Dickin on 23 Feb 1999, 7:59 AM

> > I believe that all classes should support a copy constructor:

> > public void someMethod (Foo fIsaFoo) {
> > Foo barIsAnotherFoo = new Foo(fIsaFoo);
> > ....
> > }

> > At the very least, a constructor with the signature:

> > public Foo (Foo fooToCopy);

> > should clone the argument supplied.

>
> While I would agree that a C++ canonical object would have
> a copy constructor, the clone() method is Java's way of
> accomplishing the same thing. And clone() is already a
> part of the canonical object idiom.

> I have a bit of material about clone() versus the copy
> constructor at the end of my Inner Java chapter on
> cloning:

>
> http://www.artima.com/innerjava/webuscript/cloning.html
>

> See the section, "Clone and the Copy Constructor."

> bv

For me clone is crippled.
Object cannot be cloned. To clone something it must have clone
declared as public. Vector's clone performs a shallow clone, but
it couldn't perform a deep clone because it stores Object.

You indicate that a class is cloneable by implementing the
Cloneable interface. If a derived class isn't cloneable you cannot
remove this marking interface, you have to throw
CloneNotSupportedException in clone. This is ugly because there
isn't one way of telling if an object is cloneable.

To me a better solution would be a public isCloneable method
on Object. If a class is cloneable then you override this and
return true. If you want to stop a class from being cloneable
you return false.





Replies:

Sponsored Links



Google
  Web Artima.com   
Copyright © 1996-2009 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use - Advertise with Us