Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

shallow cloning

 
priyanaka jaiswal
Ranch Hand
Posts: 79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,


How to achieve shallow cloning in java?


Thanks in advance
 
Winston Gutkowski
Bartender
Pie
Posts: 10251
58
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
priyanaka jaiswal wrote:How to achieve shallow cloning in java?

Usually with java.lang.Cloneable and clone().

Winston
 
Jelle Klap
Bartender
Posts: 1951
7
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
While that is true, I personally avoid the Cloneable interface for new types, if at all possible.
I'd much rather just implement a copy-constructor or some factory method.
 
Winston Gutkowski
Bartender
Pie
Posts: 10251
58
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jelle Klap wrote:While that is true, I personally avoid the Clonable interface for new types, if at all possible.
I'd much rather just implement a copy-constructor or some factory method.

I totally agree, but the question, as yet, isn't very clear.

And clone() does sometimes have the virtue of brevity.

Winston
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Winston Gutkowski wrote:
Jelle Klap wrote:While that is true, I personally avoid the Clonable interface for new types, if at all possible.
I'd much rather just implement a copy-constructor or some factory method.

I totally agree, but the question, as yet, isn't very clear.

And clone() does sometimes have the virtue of brevity.

Winston


And with clone() you get polymorphism--you can get a copy without having to care which implementation class you have.
 
Richard Tookey
Bartender
Posts: 1166
17
Java Linux Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm with Jeff . With clone() you can get a copy without knowing the class of the object being copied. There was an article many years ago (I can't find it now) on this subject that came down on the side of clone() but these days I gather the tide has turned. I wonder how the 'prototype' pattern is implemented using copy constructors?
 
Ritesh raushan
Ranch Hand
Posts: 100
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Generally clone method of an object, creates a new instance of the same class and copies all the fields to the new instance and returns it.
This is nothing but shallow copy. Object class provides a clone method and provides support for the shallow copy.
It returns ‘Object’ as type and you need to explicitly cast back to your original object.

Since the Object class has the clone method (protected) you cannot use it in all your classes.
The class which you want to be cloned should implement clone method and overwrite it.
It should provide its own meaning for copy or to the least it should invoke the super.clone().
Also you have to implement Cloneable marker interface or else you will get CloneNotSupportedException.
When you invoke the super.clone() then you are dependent on the Object class’s implementation and what you get is a shallow copy.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ritesh raushan wrote:The process of creating an exact copy of an existing copy of an existing object is called cloning...

There are two types of cloning..


Actually there are as many types for a given class as there are combinations of member objects in that class and all its contained objects. We can clone some or all of the members 1, 2, or N layers deep. (Not that we should do that, but the option is there.

But yes, usually we talk only about the two extremes.

1-deep cloning(when the cloned object is modified ,if the original object is not modified,then is called deep cloning).

2-shallow cloning(when the cloned object is modified,same modification will also affect the original object called shallow cloning)...


The modification effects you mention are consequences of the two types of cloning, not the definitions.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic