I have two classes: the parent class A and some subclass B, which extends A and has several more fields than A. I want to be able to clone both of these classes, so I have overridden the clone()
method from the Object
class.
Since class B is basically class A with some extra fields I would like to use the clone of class A in the clone()
implementation of class B. I have tried the something along the lines of:
public class A
{
public Object clone()
{
A cloneA = new A();
cloneA.foo = this.foo;
cloneA.bar = this.bar;
return cloneA;
}
}
public class B extends B
{
public Object clone()
{
Object cloneA = super.clone();
B cloneB = (B)cloneA;
cloneB.fieldSpecificForB = this.fieldSpecificForB;
return cloneB;
}
}
This way I would not have to duplicate all the clone logic from A's clone()
method in B's clone()
method. Unfortanely this does not work since Java does not allow an object of class A to be casted to an object of class B. I searched around for an answer on how to do this, but it seems there is no way to reuse the cloning logic from A. Does this mean that every time I add another field to A, I have to manually add the copying of this field to the clone()
method of all the subclasses. This seems to be very error-prone...