clone()
methods with return types different from the class they're located in.
Often a clone()
method will have a return type of java.lang.Object
, which makes it harder to use by its clients.
Effective Java (the second and third editions) recommends making the return type of the clone()
method the same as the
class type of the object it returns.
Example:
class Foo implements Cloneable {
public Object clone() {
try {
super.clone();
} catch (CloneNotSupportedException e) {
throw new AssertionError();
}
}
}