3

I have the following classes in C#:

public class BaseClass
{
    public virtual void DoSomethingVirtual()
    {
        Console.WriteLine("Base.DoSomethingVirtual");
    }

    public new void DoSomethingNonVirtual()
    {
        Console.WriteLine("Base.DoSomethingNonVirtual");
    }
}

public class DerivedClass : BaseClass
{
    public override void DoSomethingVirtual()
    {
        Console.WriteLine("Derived.DoSomethingVirtual");
    }

    public new void DoSomethingNonVirtual()
    {
        Console.WriteLine("Derived.DoSomethingNonVirtual");
    }
}

class ConsoleInheritanceTrial
{
    static void Main(string[] args)
    {
        Console.WriteLine("Derived via Base Reference.");

        BaseClass BaseRef = new DerivedClass();
        BaseRef.DoSomethingVirtual();
        BaseRef.DoSomethingNonVirtual();

        Console.WriteLine();
        Console.WriteLine("Derived via Dereived Reference.");

        DerivedClass DerivedRef = new DerivedClass();
        DerivedRef.DoSomethingVirtual();
        DerivedRef.DoSomethingNonVirtual();

        Console.Read();
    }
}

After running Main function, I got this:

Derived Via Base Reference
Derived.DoSomethingVirtual
Base.DoSomethingNonVirtual

Derived Via Derived Reference
Derived.DoSomethingVirtual
Derived.DoSomethingNonVirtual

Why did the baseRef.DoSoemthingNonVirtual call the base function? Has it got something to do with the "new" keyword in the Derived Class for that function? I understand the importance of "virtual" and "overrides". My confusion was caused by the statement: BaseClass BaseRef = new DerivedClass();

Christos
  • 53,228
  • 8
  • 76
  • 108
Veda
  • 243
  • 1
  • 15

3 Answers3

4

Why did the BaseRef.DoSoemthingNonVirtual call the base function?

This is your declaration:

BaseClass BaseRef = new DerivedClass();

Here you create an object of type DerivedClass and you assign a reference of it to a variable that is of type BaseClass. This can be done since, the DerivedClass has as a base type the BaseClass.

Then here

BaseRef.DoSomethingNonVirtual

you call the method DoSomethingNonVirtual.

What is the type of BaseRef?

It is BaseClass. So the method of this class is called and not the method of DerivedClass.

So the problem here is that you have created an object of type DerivedClass and you assigned the reference of this object to a variable of type BaseClass. So the CLR, when see this call for the first time

BaseRef.DoSomethingNonVirtual

has to resolve the type of BaseRef, then look the object's method table, pick the corresponding IL and at the last produce the corresponding native code. How CLR would resolve this? As an object of type BaseClass and not DerivedClass.

Christos
  • 53,228
  • 8
  • 76
  • 108
3

Why did the baseRef.DoSomethingNonVirtual call the base function?

Because the compile-time type of baseRef is BaseClass, the method call was resolved to BaseClass.DoSomethingNonVirtual(). The actual type of the object referenced by baseRef is DerivedClass, but the compiler doesn't know it.

This method is not virtual, so when it's called at run-time, the CLR will simply call the BaseClass implementation.

Jakub Lortz
  • 14,616
  • 3
  • 25
  • 39
2

If you define a virtual method and you override it in a derived class, this method will have the overwritten behaviour in all levels. A base class will use the derived method. That's the reason, why overwritten methods most of the time call their base implementation internally.

With new you define a method with the same name - not more...

The base class does not know about this method, so it won't be called.

Shnugo
  • 66,100
  • 9
  • 53
  • 114