Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
Win a copy of Learning OpenStack Networking: Build a solid foundation in virtual networking technologies for OpenStack-based clouds this week in the Cloud/Virtualization forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Knute Snortum
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Ganesh Patekar
  • Stephan van Hulst
  • Pete Letkeman
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Ron McLeod
  • Vijitha Kumara

shallow cloning  RSS feed

 
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
 
Bartender
Posts: 10575
66
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
 
Bartender
Posts: 1952
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
Posts: 10575
66
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
 
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.
 
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?
 
Ranch Hand
Posts: 101
  • 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.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!