• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

Clone Method

 
Ranch Hand
Posts: 629
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi All,

I am working on clone method and I was wondering if we have to always override it in order to use it? I've seen some places online where the method has been overridden but for me, without even overriding it, My code works fine?! I am cloning a sample class's object and not classes like ArrayList who have already overridden the clone method.

Thanks.
[ September 07, 2008: Message edited by: Arjun Reddy ]
 
Author
Posts: 3473
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You should override the clone() method very judiciously. Implementing a properly functioning clone method is complex and it is rarely necessary. You are better off providing some alternative means of object copying like serializing and deserializing (could have a performance impact), static factory method or simply not providing the capability. A better approach is to provide a copy constructor or a static factory method in place of a constructor.






Static factory method


[ September 07, 2008: Message edited by: arulk pillai ]
 
Arjun Reddy
Ranch Hand
Posts: 629
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So, the following clone method program is wrong? coz am not overriding the clone method?



Thanks.
 
Sheriff
Posts: 9708
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you implement the Cloneable interface, then the clone method of Object class will create a SHALLOW copy of your object, but if you don't implement the Cloneable interface, then the clone method of the object class throws CloneNotSupported exception.

If you want complete control over cloning your objects, then you must implement the Cloneable interface and override the clone method of the object class..
 
Ranch Hand
Posts: 300
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That means if we need a shallow copy of an object we don't need to override clone method as shown in the above post??

But if we need a deep copy then only we would override the clone method??

Kindly clarify.
 
author
Posts: 9050
21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
this is an interesting discussion, and for those of you keeping score, this topic is NOT on any version of the SCJP exam.

hth,

Bert
 
Patricia Samuel
Ranch Hand
Posts: 300
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Would anyone like answer my previous post?
 
Ankit Garg
Sheriff
Posts: 9708
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@Patricia you must override clone method if you need complete control over the cloning process. Also note that clone method in Object class is protected. So if you implement Cloneable interface and don't override clone method, no code outside the class will be able to call clone on the object of your class.

So basically to implement cloning you must override the clone method.

And as Bert said, it is not a part of SCJP so if you are only concerned with SCJP, you need not to worry about cloning...
 
Patricia Samuel
Ranch Hand
Posts: 300
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ah!! I missed this point. I tried an example using main method. I forgot that in real application, we have to use it outside the class.

Thanks Ankit.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic