From Javadoc Cloneable
By convention, classes that implement this interface must override Object.clone (which is protected) with a public method. See Object.clone () for details on overriding this method.
Please note that this interface does not contain a cloning method. Therefore, it is not possible to clone an object only because it implements this interface. Even if the clone method is activated reflectively, there is no guarantee that it will succeed.
Clone is one of the earliest projects in java and has flaws
About access - When a method is protected, it can only be accessed by the class itself, subclasses of the class, or classes in the same package as the class .
Thus, it is available in classes A and B , the way you do this is only possible if you are in the same package, which turns out to be java.lang
You can provide some method like this inside A
public A copy() throws CloneNotSupportedException { return (A) clone(); }
Correct implementation
@Override public Object clone() throws CloneNotSupportedException { return super.clone(); };
Also remember that the parent is not a type of child, so dropping from A to B will not work. A child is a type of parent, so work from B to A will work.
source share