Lets say that Class B
extends class A
and class A
is Cloneable as follows:
public class A implements Cloneable {
public Object clone() throws CloneNotSupportedException {
A ac = (A) super.clone();
return ac;
}
}
public class B extends A {
public Object clone() throws CloneNotSupportedException {
B a = (B) super.clone();
return a;
}
}
Why it is legal to perform down-cast from A to B in the next line:
B a = (B) super.clone(); // (super of B is A)
while the next down-cast is run-time error?
A a = new A();
B b = (B) a.clone();
Thanks in advance!