class A {
}
public class B extends A {
public static void main(String[] args) {
A m = new A();
B n = (B)m;
}
}
this code can not be complied. However, in the code below, this downcast works.
class A {
}
public class B extends A implements Cloneable{
@Override
public B clone() throws CloneNotSupportedException {
return (B)super.clone();
}
public static void main(String[] args) {
B m = new B();
B n = m.clone();
}
}
so, why this downcast works?
=============Correction============================
sorry for my fault, it should be B n = **(B)**m;
, not B n = m;
.
I'm very sorry. I have corrected it in the above code.
Even in first case -; class A {
}
public class B extends A {
public static void main(String[] args) {
A m = new A();
// B n = m;
B n = (B)m;
}
}
It's work.