Suppose I have this:
using System; public class Program { public static void Main() { BaseClass bc = new DerivedClass(); bc.Method1(); bc.Method2(); Console.WriteLine(bc.GetType().FullName); // Output // Derived - Method1 (override) // Base - Method2 // DerivedClass } } public class BaseClass { public virtual void Method1() { Console.WriteLine("Base - Method1"); } public virtual void Method2() { Console.WriteLine("Base - Method2"); } } public class DerivedClass : BaseClass { public override void Method1() { Console.WriteLine("Derived - Method1 (override)"); } public new void Method2() { Console.WriteLine("Derived - Method2 (new)"); } }
If the instance variable of the deriving class is cast to the base class and that instance variable is used to call the overridden methods, the method overridden with the override keyword will execute the implementation in the deriving class whereas the one overridden with the new keyword will execute the implementation in the base class.
How is the variable bc
in the above example cast to the Base Class?
I know that the new keyword will override the method implementation in the deriving class and it will be executed when an instance variable of the deriving class is used to call the overridden method, but I don't know what type of Conversion it is.. Doesn't seem to be Implicit nor Explicit, may be Type Conversion but I am confused by the syntax.
Any explanation is appreciated.