Given an Example like this
class A {
void method() {
System.out.println("A");
}
}
class B extends A{
void method() {
System.out.println("B");
}
}
class Mock1 {
public static void main(
String [] args) {
B b = new B();
A c = (A) new B();
c.method();
}
}
The output I got is B.
But inside the main() method if i have a code like this,
B b = new B();
A c = (B) new A();
c.method();
This code compiles fine, but I am getting a java.lang.ClassCastException when I try to run this code.
Can anyone explain this behaviour.
Thanks in advance.