The general suggestion: use a copy constructor. In fact, only a class itself knows how to create a clone of itself. No class can clone an instance of another class. The idea goes like this:
public class Foo {
public List<Bar> bars = new ArrayList<Bar>();
private String secret;
// Copy constructor
public Foo(Foo that) {
// new List
this.bars = new ArrayList<Bar>();
// add a clone of each bar (as an example, if you need "deep cloning")
for (Bar bar:that.bars) {
this.bars.add(new Bar(bar));
}
// clone the secret value
this.secret = new String(that.secret);
}
// ...
}
So if we want to clone a foo
, we simply create a new one based on foo
:
Foo clonedFoo = new Foo(foo);
That's the recommended way to clone an instance.
copy constructor works well with inheritance. Consider a subclass
public ChildFoo extends Foo {
private int key;
public ChildFoo(ChildFoo that) {
super(that);
this.key = that.key;
}
}
Foo
has a copy constructor and ChildFoo
simply calls it from it's own copy constructor.
Your example is possible but not advisable. What will happen:
Foo a = new Foo();
ChildFoo b = new ChildFoo(a);
This would require a constructor on ChildFoo like:
public ChildFoo(Foo that) {
// call the copy constructor of Foo -> no problem
super(that);
// but how to initialize this.key? A Foo instance has no key value!
// Maybe use a default value?
this.key = 0;
}
Technically not a challenge but b
is not a clone of a
because the objects don't have the same type. So this (your example) is not cloning.