<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
		>
<channel>
	<title>Comments on: Java Clone, Shallow Copy and Deep Copy</title>
	<atom:link href="http://javapapers.com/core-java/java-clone-shallow-copy-and-deep-copy/feed/" rel="self" type="application/rss+xml" />
	<link>http://javapapers.com/core-java/java-clone-shallow-copy-and-deep-copy/</link>
	<description>BLOG on core java, servlets, JSP, design patterns interview questions with quality answers, source code and discussions.</description>
	<lastBuildDate>Tue, 07 Sep 2010 14:43:42 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9</generator>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
		<item>
		<title>By: Rob Poole</title>
		<link>http://javapapers.com/core-java/java-clone-shallow-copy-and-deep-copy/comment-page-1/#comment-1981</link>
		<dc:creator>Rob Poole</dc:creator>
		<pubDate>Mon, 23 Aug 2010 20:06:09 +0000</pubDate>
		<guid isPermaLink="false">http://javapapers.com/?p=191#comment-1981</guid>
		<description>Nice, but it wasn&#039;t too hard to notice that you name-dropped Jamie Zawinski in your source code (right down to using his usual Internet handle, jwz, as a variable name).  Now, it&#039;s possible you wrote this code all on your own and you thought it would be cheeky fun to name-drop someone who&#039;s a co-founder of both Netscape and Mozilla.org, but based on my experiences, I find it more likely that the code was plagiarized from someone else&#039;s example.

This makes me wonder what else was plagiarized, if the example code looks suspect.  Again, maybe I&#039;m wrong, but I&#039;m just saying what it looks like to me.  I&#039;ve seen a lot of tech blogs and websites out there that recycle other people&#039;s content as their own, often without even giving proper attribution (and in some cases charging for access to content that isn&#039;t theirs in the first place!).

Now, if you *did* write that code... well, I hope JWZ isn&#039;t a litigious sort of person.  It&#039;s kind of considered bad form to drop someone else&#039;s name in sample code, unless you have a close relationship with that person and/or you have some kind of rivalry with them.

Now, as to your article&#039;s contents...

&quot;When implementing a singleton pattern, if its superclass implements a public clone() method, to prevent your subclass from using this class’s clone() method to obtain a copy overwrite it and throw a CloneNotSupportedException.&quot;

The word here is override, not overwrite.  Big difference in semantics.

&quot;One more disadvantage with this clone system is that, most of the interface / abstract class writers in java forget to put a public clone method.&quot;

They didn&#039;t &quot;forget&quot; to do it.  The clone method belongs to the Object class, and must be overridden.  Abstract classes will have this method inherited from Object, but it&#039;s a protected method, and the abstract class -- by definition -- won&#039;t have all details implemented that a concrete subclass has.  The point is, an abstract superclass would need to know implementation details of concrete subclasses, which is a big no-no, just in order to implement clone().  Better to defer that implementation to the concrete subclass, which may possess fields and other methods not present in the abstract superclass.

By the same token, it should now be obvious why an interface can not contain clone() as a method -- first, there is already a protected clone() method in Object which you must override, and secondly, the Cloneable interface is what marks an object as being capable of being cloned, but an interface (the hypothetical interface we&#039;re talking about, not the Cloneable interface) can&#039;t make guarantees about the implementation logic of classes that implement it. The implementing class still needs to override Object.clone() no matter what.</description>
		<content:encoded><![CDATA[<p>Nice, but it wasn&#8217;t too hard to notice that you name-dropped Jamie Zawinski in your source code (right down to using his usual Internet handle, jwz, as a variable name).  Now, it&#8217;s possible you wrote this code all on your own and you thought it would be cheeky fun to name-drop someone who&#8217;s a co-founder of both Netscape and Mozilla.org, but based on my experiences, I find it more likely that the code was plagiarized from someone else&#8217;s example.</p>
<p>This makes me wonder what else was plagiarized, if the example code looks suspect.  Again, maybe I&#8217;m wrong, but I&#8217;m just saying what it looks like to me.  I&#8217;ve seen a lot of tech blogs and websites out there that recycle other people&#8217;s content as their own, often without even giving proper attribution (and in some cases charging for access to content that isn&#8217;t theirs in the first place!).</p>
<p>Now, if you *did* write that code&#8230; well, I hope JWZ isn&#8217;t a litigious sort of person.  It&#8217;s kind of considered bad form to drop someone else&#8217;s name in sample code, unless you have a close relationship with that person and/or you have some kind of rivalry with them.</p>
<p>Now, as to your article&#8217;s contents&#8230;</p>
<p>&#8220;When implementing a singleton pattern, if its superclass implements a public clone() method, to prevent your subclass from using this class’s clone() method to obtain a copy overwrite it and throw a CloneNotSupportedException.&#8221;</p>
<p>The word here is override, not overwrite.  Big difference in semantics.</p>
<p>&#8220;One more disadvantage with this clone system is that, most of the interface / abstract class writers in java forget to put a public clone method.&#8221;</p>
<p>They didn&#8217;t &#8220;forget&#8221; to do it.  The clone method belongs to the Object class, and must be overridden.  Abstract classes will have this method inherited from Object, but it&#8217;s a protected method, and the abstract class &#8212; by definition &#8212; won&#8217;t have all details implemented that a concrete subclass has.  The point is, an abstract superclass would need to know implementation details of concrete subclasses, which is a big no-no, just in order to implement clone().  Better to defer that implementation to the concrete subclass, which may possess fields and other methods not present in the abstract superclass.</p>
<p>By the same token, it should now be obvious why an interface can not contain clone() as a method &#8212; first, there is already a protected clone() method in Object which you must override, and secondly, the Cloneable interface is what marks an object as being capable of being cloned, but an interface (the hypothetical interface we&#8217;re talking about, not the Cloneable interface) can&#8217;t make guarantees about the implementation logic of classes that implement it. The implementing class still needs to override Object.clone() no matter what.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Anonymous</title>
		<link>http://javapapers.com/core-java/java-clone-shallow-copy-and-deep-copy/comment-page-1/#comment-1953</link>
		<dc:creator>Anonymous</dc:creator>
		<pubDate>Wed, 18 Aug 2010 12:03:39 +0000</pubDate>
		<guid isPermaLink="false">http://javapapers.com/?p=191#comment-1953</guid>
		<description>three disadvantage given is cool...

Dipanshu</description>
		<content:encoded><![CDATA[<p>three disadvantage given is cool&#8230;</p>
<p>Dipanshu</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: manoj</title>
		<link>http://javapapers.com/core-java/java-clone-shallow-copy-and-deep-copy/comment-page-1/#comment-801</link>
		<dc:creator>manoj</dc:creator>
		<pubDate>Fri, 22 Jan 2010 21:58:43 +0000</pubDate>
		<guid isPermaLink="false">http://javapapers.com/?p=191#comment-801</guid>
		<description>useful article and thanks for posting it
Can you give an example for this &quot;When implementing a singleton pattern, if its superclass implements a public clone() method, to prevent your subclass from using this class’s clone() method to obtain a copy overwrite it and throw a CloneNotSupportedException.&quot;</description>
		<content:encoded><![CDATA[<p>useful article and thanks for posting it<br />
Can you give an example for this &#8220;When implementing a singleton pattern, if its superclass implements a public clone() method, to prevent your subclass from using this class’s clone() method to obtain a copy overwrite it and throw a CloneNotSupportedException.&#8221;</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Joe</title>
		<link>http://javapapers.com/core-java/java-clone-shallow-copy-and-deep-copy/comment-page-1/#comment-411</link>
		<dc:creator>Joe</dc:creator>
		<pubDate>Sun, 06 Dec 2009 05:21:36 +0000</pubDate>
		<guid isPermaLink="false">http://javapapers.com/?p=191#comment-411</guid>
		<description>Application of shallow copy or deep copy depends on business needs. Unless your business logic explicitly needs deep copy, you can always go for shallow copy.</description>
		<content:encoded><![CDATA[<p>Application of shallow copy or deep copy depends on business needs. Unless your business logic explicitly needs deep copy, you can always go for shallow copy.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: som</title>
		<link>http://javapapers.com/core-java/java-clone-shallow-copy-and-deep-copy/comment-page-1/#comment-407</link>
		<dc:creator>som</dc:creator>
		<pubDate>Sat, 05 Dec 2009 20:31:44 +0000</pubDate>
		<guid isPermaLink="false">http://javapapers.com/?p=191#comment-407</guid>
		<description>That was a valuable suggestion Joe. Thank you so much. I have another doubt. Can you please list some applications of shadow and deep copy or where they might be used?</description>
		<content:encoded><![CDATA[<p>That was a valuable suggestion Joe. Thank you so much. I have another doubt. Can you please list some applications of shadow and deep copy or where they might be used?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Joe</title>
		<link>http://javapapers.com/core-java/java-clone-shallow-copy-and-deep-copy/comment-page-1/#comment-406</link>
		<dc:creator>Joe</dc:creator>
		<pubDate>Sat, 05 Dec 2009 20:03:04 +0000</pubDate>
		<guid isPermaLink="false">http://javapapers.com/?p=191#comment-406</guid>
		<description>Som, you have not cloned the object. That is you have not acquired a copy of the object. Instead you have assigned the reference of that object to a new variable. Eventually pointing to same memory location. Therefore you get the same output!

In my code I get the same output because java clone not only copies object meta data, it also copies value of attributes.

I suggest you an experiment! Add one more attribute to Employee class. After clone set a value to original object&#039;s attribute. Now print new object&#039;s attribute and see if that value is reflected. Do this in my code and then in your code. This will help to clear your doubt on java clone, shallow copy and deep copy.</description>
		<content:encoded><![CDATA[<p>Som, you have not cloned the object. That is you have not acquired a copy of the object. Instead you have assigned the reference of that object to a new variable. Eventually pointing to same memory location. Therefore you get the same output!</p>
<p>In my code I get the same output because java clone not only copies object meta data, it also copies value of attributes.</p>
<p>I suggest you an experiment! Add one more attribute to Employee class. After clone set a value to original object&#8217;s attribute. Now print new object&#8217;s attribute and see if that value is reflected. Do this in my code and then in your code. This will help to clear your doubt on java clone, shallow copy and deep copy.</p>
]]></content:encoded>
	</item>
</channel>
</rss>
