I am trying to implement a clone() method on a DoubleLinkedList. Now, the problem is that implementing it by “the convention” is a lot more troublesome than just creating a new DoubleLinkedList and filling it with all the elements of my current DoubleLinkedList.
Is there any inconvenient I am not seeing when doing that?
Here is my current approach:
@Override
public DoubleLinkedList<T> clone() {
DoubleLinkedList<T> dll = new DoubleLinkedList<T>();
for (T element : dll) {
dll.add(element);
}
return dll;
}
Here is what it would be by the convention:
@Override
public DoubleLinkedList<T> clone() {
try {
DoubleLinkedList<T> dll = (DoubleLinkedList<T>)super.clone();
//kinda complex code to copy elements
return dll;
} catch (CloneNotSupportedException e) {
throw new InternalError(e.toString());
}
}
As you correctly point out, the convention is to always call
super.clone()in the beginning of an implementation ofclone(). From the API docs onObject#clone():Your first attempt (without using
super.clone()) has the following problem:Suppose I have
(and that
IntDoubleLinkedListdoes not bother to overrideclone()) and I run the following code:What will happen? The clone method of your
DoubleLinkedListwill be executed, which, if it doesn’t go through super.clone(), returns an instance ofDoubleLinkedListwhich in turn can not be casted to anIntDoubleLinkedList. AClassCastExceptionwill be thrown!So how does
super.clone()solve this issue? Well, if everybody stick to the convention of callingsuper.clone()in an overriden clone method,Object.clone()will eventually be called, and this implementation will create an instance of a proper type (IntDoubleLinkedListin this case)!