I'm trying to implement the clone of a derived class, but , I didn't get, I don't know why. Why can't I clone the class B?, I get an CastClassExpcetion.
public class A implements Cloneable {
private Integer a;
@Override
protected Object clone() throws CloneNotSupportedException {
A clone = new A(); //Why if I do this instance of super.clone() I get an exception?
A clone = (A) super.clone();
clone.a = this.a;
return clone;
}
..
}
public class B extends A implements Cloneable {
private String b;
..
@Override
protected Object clone() throws CloneNotSupportedException {
B clone = (B) super.clone();
clone.b = this.b;
return clone;
}
public static void main(String[] args) throws CloneNotSupportedException {
B b = new B(1, "s");
B clone = b.clone();
}
}