Why do we implement Cloneable, even if we can go for deep cloning using the following snippet

public class Color { String color; Color(String color) { this.color=color; } } public class ColoredCircle { int x; Color color; ColoredCircle(int x, Color color) { this.x=x; this.color=color; } public Object testClone() { Color c = new Color(this.color.color); ColoredCircle cc1 = new ColoredCircle(this.x, c); return cc1; } } 

In the ColoredCircle class mentioned above, we have a testClone () method that works exactly the same as Deep Cloning. Now I am confused by the fact that it is necessary to implement Cloneable to clone? And is the above program a kind of deep cloning?

+5
source share
3 answers

Do I need to implement a Cloneable clone ? Yes. The clone () method has a secure access modifier with the following Javadoc extension: -

This method creates a new instance of the class of this object and initializes all its fields with the exact contents of the corresponding fields of this object, as if it were assigned; the contents of the fields are not cloned per se. Thus, this method performs a shallow copy this object, and not a deep copy operation.

Your testClone method, although it may be correct in the behavior of cloning, is not in itself a cloning object. The cloned object must implement the Cloneable interface and preferably have open access for clone() so that it can be used outside the class.

It will be difficult for testClone() involved in your class to understand the importance of the testClone() method.

+1
source

The implementation of the Cloneable interface is necessary so that the call to Object.clone() does not raise an exception. (That is the whole goal of Cloneable .)

You are not using Object.clone() . Therefore, the implementation of Cloneable or has no effect. This has nothing to do with what is called your method. Your method can be called testClone() , and it can call up to super.clone() . Or your method can be called clone() , and it cannot use super.clone() . The important thing is that you are not using Object.clone() .

The advantage of using Object.clone() is that the object it returns has the same execution class as the object it is called on. On the other hand, your method always creates a new ColoredCircle object. Therefore, when your testClone() method is inherited in a subclass, it will still create ColoredCircle , not an instance of that subclass. If your method is called super.clone() , it will be able to get an instance of any subclass of the current instance.

+2
source

Cloneable is a marker interface. It does not contain methods.

The fact is that the clone method is defined in the Object class. Since all classes implement the Object class, this means that all classes have a clone method. However, not all objects actually support it. Some of them just throw a CloneNotSupportedException . But this clone method is a native method, and for the exact behavior of this method is not displayed in the java source code. Thus, he lacks transparency.

The Cloneable will help us understand which classes are actually cloned and which are not. By convention, classes that do not implement Cloneable will throw a CloneNotSupportedException .

Note. The clone method is also marked protected . That way, he also agrees to override it to make it public in supporting classes.


The clone design was introduced in JDK1. These days, the general consensus is that the java clone design contains some flaws. Some people prefer to simply create a clone constructor (for example, public Color(Color toCopy) {this.color = toCopy.color;} )

0
source

Source: https://habr.com/ru/post/1233703/


All Articles