1. In
A x = new A();
x
is an instantiation of A
and of type A
.
whereas in
A x = new B();
x
is an instantiation of B
and of type A
.
2. The important thing to note here is that (in the second case) if you call x.someMethod()
, the method of B
will be called, not the method of A
(this is called dynamic binding, as opposed to static binding). Furthermore, casting changes only the type, so
A x = new B();
((A)x).run_function(); // Need extra parenthesis!
will still call B
's method.
As I said above, you need to include those extra parenthesis since
(A)x.run_function();
is equivalent to
(A)(x.run_function());