Is the Java clone () method the only way to achieve polymorphic cloning?

I need to equip my class with polymorphic cloning (deep copy), i.e. I need something like this:

SuperType original = new SubType(); SuperType copy = original.clone(); 

where original.clone() can be replaced by any mechanism for creating a deep copy, and the actual copy type must be SubType , because original also SubType .

Is the clone() and Cloneable method the only way to achieve this? Factory methods and copy constructors cannot be used since the actual class is known only at runtime, right? Are there any other suggested methods besides those used for serialization-deserialization, and the Java deep cloning library , which is IMHO even worse than black magic than the clone() method?

Thanks Peter

+6
source share
4 answers

Actually, the Object clone() method does not allow you to make any polymorphic call, because it is protected . The Cloneable implementation is useless either because it does not contain the clone() method.

You can perform polymorphic cloning by providing a polymorphic method for cloned classes, which in turn call the copy constructor.

 abstract class SuperType { public SuperType(SuperType t) { } public abstract SuperType deepCopy(); } class SomeType extends SuperType { SomeType(SomeType t) { //copy constructor } @Override public SomeType deepCopy() { return new SomeType(this); } } ... SuperType original = new SubType(); SuperType copy = original.deepCopy(); //the deepCopy is called on children classes 

See also:

Joshua Block cloning and copy constructor views

+4
source

For your code to work, this code is either inside a SuperType class or subclass, or your SuperType type must have a public clone() method. The public clone() method does not exist automatically, and you must implement it. You could call it something else in this regard, for example. copy() .

The question then becomes how to implement this clone() method (or what to call it) so that it performs polymorphic cloning.

What was intended by the Java developers was to call super.clone() , assuming that all classes in the inheritance hierarchy are similarly implemented by clone() to perform polymorphic cloning. This ultimately comes to the protected Object.clone() method, which does the magic of polymorphic cloning. Note that for Object.clone() , in order not to throw an exception, your class must implement the Cloneable interface.

However, there are other possible methods. For example, assuming all subclasses have a default constructor, you can do something like this.getClass().newInstance() . This will create an object of the right class, but the fields will not be copied. Your clone method will have to copy all the fields, and subclasses will have to override your clone method to copy their fields, etc. Note that in this case it does not matter if the Cloneable interface is implemented.

Another way is that the class is Serializable, serialized and unserialize this . This should create a polymorphic clone that transfers all serializable fields.

+2
source

You can simply add the copy method to SuperType and implement it in all SubTypes

 class SuperType { public SuperType copy() { ... } } 
+1
source

If you do not like the Cloneable interface, you can create your own interface. Each class will be responsible for creating a new instance of the correct type.

0
source

All Articles