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 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> 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 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' inte= rface, but not for your custom implementation. - You must name a constructor the same as the class it instantiates, and cl= ass names by convention should begin with an upper-case letter, so your con= structor 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 an= d 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 lat= ter 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. Th= at is why you sometimes need a copy constructor and/or a copy method in add= ition to or instead of 'clone()'. - Reimplementing 'clone()' is reinventing the wheel. Your solution will ha= ve no advantages over what's already provided, and will have disadvantages.= =20 - Code that flouts standards for no apparent reason is hard to maintain. D= on't do that.=20 --=20 Lew