Search code examples
javainheritanceclone

Implementing clone() in a subclass


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...


Solution

  • The way clone works is, is that it copies all fields for you automatically. You must however call super.clone() for it to work:

    public class A implements Cloneable  // add 'implements'
    {
      public Object clone()
      {
        A cloneA = super.clone(); // this copies all fields and returns something of *the same type* as 'this'...
        return cloneA;
      }
    }
    
    public class B extends A //supposed to extend 'A' right?
    {
      public Object clone()
      {
        Object cloneA = super.clone(); // this returns object of runtime type B
        B cloneB = (B)cloneA; // will work
        return cloneB;
      }
    }
    

    Edit: Actually your clone methods do nothing now, so it is equivalent to write:

    public class A implements Cloneable  //add 'implements'
    {
    
    }
    
    public class B extends A //supposed to extend 'A' right?
    {
    
    }
    

    The implements Cloneable part is the one that does the trick. See also Cloneable.