• 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:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Using objects clone() method

 
Ranch Hand
Posts: 210
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I hava a data object which I want to be able to clone. The clone method will instantiate a new data object without copying the data of the original.

Like this;

package a;
public interface Data
{
//methods
}

package a;
public abstract class AbstractData implements Data
{

public Object clone()
{
//reflection thing
return newData;
}
}

public class myData extends AbstractData
{
//Methods
}

Ok, the problem is, that:

public Data getEmptyObject(Data data)
{
Data newObject = data.clone();
}

This doesn't work, I get a 'The method clone() from the type Object is not visible' . First, I don't understand why this is happening. They are all in the same package, so even if clone() is not defined public in the interface, I must be able to call the protected method.

What is the solution to work this out ?

A) placing 'Object clone()' in the interface ? (I feel bad about this, dont know why)

B) do not use the clone method and use an self made method (like cloneData or something) which I put in the interface.

C) ... ?
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You have to do two things to the class you want to be able to clone:

One, override clone() to make it public, and just call "return super.clone()" in your implementation.

Two, declare that the class implements the Cloneable interface.

Why is this necessary, you ask? Because that's how it was designed. It is a good design? Depends on who you ask. It's too complicated, most people agree.
 
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Note that the default implementation of clone() only does a shallow copy. In otherwords, if your class has member reference fields, the referenced objects are not cloned. The copy will contain references to the same objects. In order to perform a so-called deep copy, you need to make sure each of the member objects are cloned as well.

Layne
 
reply
    Bookmark Topic Watch Topic
  • New Topic