Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.help > #1191

Re: clone() and Cloneable

From Lew <lewbloch@gmail.com>
Newsgroups comp.lang.java.help
Subject Re: clone() and Cloneable
Date 2011-10-05 06:58 -0700
Organization http://groups.google.com
Message-ID <29781577.114.1317823099595.JavaMail.geo-discussion-forums@prfp13> (permalink)
References <9852a7f5-75e0-49e9-9538-93824d185421@b6g2000vbz.googlegroups.com>

Show all headers | View raw


John Goche wrote:
> What are the advantages of implementing the Cloneable
> interface as opposed to a standard copy constructor perhaps
> called clone() with no Cloneable interface in the class declaration?

- There is already machinery in the language to handle the 'Cloneable' interface, but not for your custom implementation.
- You must name a constructor the same as the class it instantiates, and class names by convention should begin with an upper-case letter, so your constructor should never be called 'clone'.
- There is already a 'clone()' method in 'Object', inherited by every type.  Calling a method 'clone()' with the same signature as the 'Object' method perforce makes it an override, thus requiring the 'Cloneable' interface and all the rest of the inbuilt machinery.  Making it not override-compatible would be just plain confusing and wrong.
- Copy constructors work in the opposite direction from 'clone()'.  The latter makes a copy of 'this', the former copies another instance into 'this'.  So the two are not equivalent, and one cannot stand in for the other.  That is why you sometimes need a copy constructor and/or a copy method in addition to or instead of 'clone()'.
- Reimplementing 'clone()' is reinventing the wheel.  Your solution will have no advantages over what's already provided, and will have disadvantages. 
- Code that flouts standards for no apparent reason is hard to maintain.  Don't do that. 

-- 
Lew

Back to comp.lang.java.help | Previous | NextPrevious in thread | Next in thread | Find similar


Thread

clone() and Cloneable John Goche <johngoche99@googlemail.com> - 2011-10-05 05:44 -0700
  Re: clone() and Cloneable Roedy Green <see_website@mindprod.com.invalid> - 2011-10-05 06:28 -0700
  Re: clone() and Cloneable Lew <lewbloch@gmail.com> - 2011-10-05 06:58 -0700
  Re: clone() and Cloneable markspace <-@.> - 2011-10-05 09:24 -0700

csiph-web