Cloning objects in Java [3 questions]

Will Asub Cloning Method Be Called? Or is Asub deeply cloned? If not, is there a way to provide a deep Asub clone using this method?

abstract class Top extends TopMost { protected Object clone() { Object obj = super.clone(); // deep copy and try catch } } abstract class A extends Top { protected Object clone() { Object obj = super.clone(); // deep copy and try catch } } class Asub extends A { protected Object clone() { Object obj = super.clone(); // deep copy and try catch } public void doSomethingNew() { } } abstract class TopMost { public void someMethod() { Top a = (Top) super.clone(); // more code here } } public class Main { public static void main(String... args) { Asub class1 = new Asub(); class1.someMethod(); } } 
0
source share
3 answers

By providing all the abstract subclasses that implement super.clone() , they essentially do nothing (since all your abstract classes in your example do nothing) and simply call (at the end) the Object.clone() method.

My suggestion is to allow all specific classes (e.g. ASub) to override the clone method and use the copy constructor construct to create an exact clone of itself ....

eg.

 public abstract class TopMost { public TopMost(TopMost rhs) { } } public abstract class Top extends TopMost { public Top(Top rhs) { super(rhs); //whatever you need from rhs that only is visible from top } } public abstract class A extends Top { public A (A rhs) { super(rhs); //TODO: do rhs copy } } public class ASub extends A { public ASub(ASub rhs) { super(rhs); //TODO: copy other stuff here.... } public Object clone() { return new ASub(this); } } 

PS Make TopMost Cloneable

+1
source

First of all, note that the clone () interface does not work , so it cannot be used in the new code. It is better to implement a copy constructor instead .

However, if you really need to do this, the correct way for TopMost implement Cloneable . What for? Says Effective Java 2nd Edition, paragraph 11:

So what does Cloneable do, given that it contains no methods? It defines the behavior of Object s of the protected clone implementation: if the class implements Cloneable , the Object s clone method returns a field copy of the object; otherwise, it throws a CloneNotSupportedException . This is a very atypical use of interfaces, not for emulation. Typically, an interface implementation says something about what a class can do for its clients. In the case of Cloneable , it changes the behavior of the protected method on the superclass.

In addition, Asub.clone must be declared public , and not protected - otherwise you cannot call it from the outside world. Also, if you use Java5 or higher, for Asub.clone is legal and it is advisable to return Asub rather than Object (and similarly for its superclasses).

You do not show members in classes - clone implementations in different classes may be different, depending on the types of members of this class. Namely, if a class has any mutable members, you need to carefully copy them, otherwise you will get different objects that share their internal state.

However, if your classes have only primitive or immutable fields, cloning works as expected, although your abstract classes have many unnecessary clone methods, all of which just call super.clone() - you can only be turned off better with Asub.clone() .

As a side note, if Top a = (Top) super.clone() not a typo, you introduce a dependency on the base class to the derived class, which is not a good idea.

+3
source

Calling super.clone() disables the virtual mechanism, so it only calls Object.clone()

0
source

All Articles