class MyCls implements Cloneable {
@Override
protected MyCls clone() throws CloneNotSupportedException {
return new MyCls(//...
}
}
The above code doesn't have any problem. So why does CopyOnWriteArrayList#clone
returns an Object
instead of CopyOnWriteArrayList
? Compiler cries when casting back from Object
to desired type. What might be the reason for this design decision after all? I see this pattern repeated all over the library.
Ralph suggested that the following code is valid instead of above code:
@Override MyCls clone() throws CloneNotSupportedException {
MyCls clone = (MyCls)super.clone();
clone.x = this.x; //or what ever to do return clone
// ...
}
The question still remains the same. Why does CopyOnWriteArrayList#clone
returns Object
instead of itself?