It's actually part of one interview question I got confused.
class A
{
public A()
{
System.out.println("A") ;
}
}
class B extends A
{
public B()
{
System.out.println("B") ;
}
}
A a1 = new B();
System.out.println() ;
A a2 = (A) new B() ;
So the question is what is the print out?
At first I thought it should print out like
B
A
B
A
But after I run at home, it gives
A
B
A
B
I understand it's inheritance and then upcasting B to A, and it's legal syntax as well, but why is A print before B?