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


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

Re: clone() and Cloneable

Path csiph.com!x330-a1.tempe.blueboxinc.net!newsfeed.hal-mli.net!feeder3.hal-mli.net!nx01.iad01.newshosting.com!newshosting.com!news-out.readnews.com!news-xxxfer.readnews.com!postnews.google.com!glegroupsg2000goo.googlegroups.com!not-for-mail
From Lew <lewbloch@gmail.com>
Newsgroups comp.lang.java.help
Subject Re: clone() and Cloneable
Date Wed, 5 Oct 2011 06:58:19 -0700 (PDT)
Organization http://groups.google.com
Lines 28
Message-ID <29781577.114.1317823099595.JavaMail.geo-discussion-forums@prfp13> (permalink)
References <9852a7f5-75e0-49e9-9538-93824d185421@b6g2000vbz.googlegroups.com>
Reply-To comp.lang.java.help@googlegroups.com
NNTP-Posting-Host 216.239.45.130
Mime-Version 1.0
Content-Type text/plain; charset=ISO-8859-1
Content-Transfer-Encoding quoted-printable
X-Trace posting.google.com 1317823190 12729 127.0.0.1 (5 Oct 2011 13:59:50 GMT)
X-Complaints-To groups-abuse@google.com
NNTP-Posting-Date Wed, 5 Oct 2011 13:59:50 +0000 (UTC)
In-Reply-To <9852a7f5-75e0-49e9-9538-93824d185421@b6g2000vbz.googlegroups.com>
Complaints-To groups-abuse@google.com
Injection-Info glegroupsg2000goo.googlegroups.com; posting-host=216.239.45.130; posting-account=CP-lKQoAAAAGtB5diOuGlDQk0jIwmH0T
User-Agent G2/1.0
X-Google-Web-Client true
Xref x330-a1.tempe.blueboxinc.net comp.lang.java.help:1191

Show key headers only | 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