I'm reading carefully Effective Java (by Joshua Bloch) and I found the following sentence on cloning:
If you design a class for inheritance, be aware that if you choose not to provide a well-behaved protected clone method, it will be impossible for subclasses to implement Cloneable.
I'm a little bit confused, because in my little test:
public class Cloning {
public static void main(String[] args) throws CloneNotSupportedException {
Woman woman1 = new Woman("Marie Curie-Sklodowska", 33, "Physics");
Woman woman2 = (Woman) woman1.clone();
}
static abstract class Person {
protected String name;
protected int age;
Person(String name, int age) {
this.name = name;
this.age = age;
}
}
static class Woman extends Person implements Cloneable {
private String field;
Woman(String name, int age, String field) {
super(name, age);
this.field = field;
}
@Override
protected Object clone() throws CloneNotSupportedException {
return super.clone();
}
}
}
I'm not getting any error. I think, I do not understand the sentence correctly. Could someone please explain, what the author had in mind?