It's just for illustration.
It doesn't really work as an example is you have
BaseClass bc = new BaseClass();
bc.MyMethod();
DerivedClass dc = new DerivedClass();
dc.MyMethod();
Giving:
BaseClass MyMethod called
DerivedClass MyMethod called
Because you could get that result without deriving any class at all!
If you write the above as
BaseClass example = new BaseClass();
example.MyMethod();
example = new DerivedClass();
example.MyMethod();
And it gives the same result (which is does) that that shows that inheritance works (a derived class instance reference can be assigned to a base class variable) and that the system sorts out the overriding for you by calling the appropriate method for the actual instance without referring to the variable type.
When you do the same with
new
methods it does look at the variable type:
Base bc = new Base();
Derived dc = new Derived();
bc.MyMethod();
dc.MyMethod();
bc = dc;
bc.MyMethod();
And you get:
Base: MyMethod called
Derived: MyMethod called
Base: MyMethod called
which shows it.