4

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

phuibers
  • 1,239
  • 11
  • 11
  • http://stackoverflow.com/a/11541044/829571 – assylias Aug 31 '12 at 10:21
  • 1
    possible duplicate of [Effective Java: Analysis of the clone() method](http://stackoverflow.com/questions/11540792/effective-java-analysis-of-the-clone-method) – assylias Aug 31 '12 at 10:21
  • Like @Mathias suggested the issue is that B is not extending class A. If you are not able to extend then you should do the copy. – dan Aug 31 '12 at 10:31
  • I see I omitted some code in my original post. I changed it to convey more closely to what I tried to do in my code. – phuibers Aug 31 '12 at 11:02

2 Answers2

7

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.

ericek111
  • 575
  • 7
  • 15
Mathias Schwarz
  • 7,099
  • 23
  • 28
  • Thanks for your answer. I noticed I made some errors in my OP (mainly B not extending A and A.clone() not returning anything). It seems I misunderstood the way in which the Cloneable interface and super.clone() work. I'll try to implement it right this time using your example. – phuibers Aug 31 '12 at 11:04
2

The problem is your implementation of A#clone(). You should never create the instance you will return in the clone method by using new, but always by calling super.clone(), exactly to avoid the error you are now experiencing.

So adjust your class A to

public class A
{
  @Override
  protected Object clone() throws CloneNotSupportedException
  {
    A cloneA = (A)super.clone();
    cloneA.foo = this.foo;
    cloneA.bar = this.bar;
    return cloneA;
  }
}
Robin
  • 36,233
  • 5
  • 47
  • 99
  • If all classes up the hierarchy construct the new object by calling `super.clone()`, yours should to, but if any of them do it by calling a copy constructor, yours must do so as well. I find irksome the notion that one should use copy constructors because `clone` is fundamentally broken, since the whole *reason* it's "broken" in the first place is the belief that copy constructors are somehow better. – supercat Jul 12 '13 at 17:57