Let's consider modified code:
public Class A {
public virtual void Print() { Console.WriteLine("Hello From A"); }
}
public Class C : A{
public override void Print() { Console.WriteLine("Hello From C"); }
}
A someObject = new C();
someObject.Print();
Optionally,
A.Print
could be
abstract
. Then and only then you will get what you want.
This mechanism is the heart of OOP and is called
late binding. This is not yet
polymorphism, which takes place when you have a whole set of object known by their
compile-time type of the base class, and
runtime type are different (hence
poly), then they can be processed on the common basis, only through the interface
provided by a base type of the set, without knowing of the runtime types.
For some explanation of this core mechanism of OOP, please see my past answer:
I couldn't figure out this portion of C# code[
^].
—SA