0

Having a requirement to copy two objects of different types. But these objects are having the same set of elements. One way is to iterate one by one element and copy the elements. But its tedious when class contains large number of elements and also contains Collection elements.

Is there any other better solution is available for this problem ?

Rosh
  • 1,676
  • 4
  • 21
  • 35

1 Answers1

0

Deep cloning is independent of the original and making changes in clone should not affect original. Nothing from the original will be affected by the deep clone. Hope this helps.

protected Object clone() throws CloneNotSupportedException {
    Foo cloned = (Foo)super.clone();
    cloned.setSomething((Something)cloned.getSomething().clone());
    return cloned;
}
Adam
  • 2,422
  • 18
  • 29