4

Quite confused with the rules of virtual inheritance when i have a method which prevents a derived class from overriding the virtual method defined in the base class. Here is a bit of code to explain my problem better :

using System;

namespace ConsoleApplication1
{
    public class A
    {
        public virtual void DoWork()
        {
            Console.WriteLine("A.DoWork()");
        }
    }
    public class B : A
    {
        public override void DoWork()
        {
            Console.WriteLine("B.DoWork()");
        }
    }

    public class C : B
    {
        public sealed override void DoWork()
        {
            Console.WriteLine("C.DoWork()");
        }
    }

    public class D : C
    {
        public new void DoWork()
        {
            Console.WriteLine("D.DoWork()");
        }
    }

    public class MyMainClass
    {
        public static void Main()
        {
            B b = new D();
            b.DoWork();

            C c = new D();
            c.DoWork();

            A a = new D();
            a.DoWork();

            Console.WriteLine("Press any key to exit");
            Console.ReadKey();
        }
    }
}

OUTPUT

C.DoWork()
C.DoWork()
C.DoWork()
Press any key to exit

It's true that if a variable of type B is used to access an instance of C as B b = new C(); b.DoWork() would result in calling C's implementation of DoWork() since C overrides the virtual DoWork() of A.

But why is it when a variable of type C, B, or A is used to access an instance of D as

B b = new D();
C c = new D();
A a = new D();

a call to DoWork() on each one of them will call the implementation of DoWork() on class C?

mrdoubtful
  • 429
  • 2
  • 7
  • 17
  • @PrestonGuillot probably not - looks like OP simply don't understand `virtual` and question just happen to mention `sealed`/`new`. – Alexei Levenkov Dec 06 '15 at 18:19
  • I'm voting to close this question as off-topic because OP is trying to do opposite to what language designed to do – T.S. Dec 07 '15 at 00:36
  • As well as http://stackoverflow.com/questions/4152049/sealed-method-in-c-sharp – Cheng Chen Dec 07 '15 at 05:52

1 Answers1

2

Class D now has two methods called DoWork.

The first one (method #1) is the virtual method defined in class A and overridden in class C. D inherits this method from C.

The second one (method #2) is a non-virtual method that is defined in class D itself. This method is totally different from method #1.

Now, you cannot access method #1 from a variable of type D because method #2 hides method #1.

However, you can still access method #1 by using a variable of type A, B or C.

To make this more clear, here is an example:

D var_d = new D();

B var_b = var_d;

var_d.DoWork(); //This accesses method #2 on an object of type D

var_b.DoWork(); //This accesses method #1 on the same object
Yacoub Massad
  • 27,509
  • 2
  • 36
  • 62
  • Hey! Thx a lot! A few clarifications btw ... 1) var_b is a variable of type B having values of that of type D having access to only the overridden method of class C (method #1) and not the overridden methods of classes B and C (just thinking since D is a subclass of C which is a subclass of B, so doesn't it inherit overridden methods of both B and C?) 2) Also, why is var_b is only interested in method #1 and not method #2 – mrdoubtful Dec 06 '15 at 19:03
  • 1) Don't you mean B and A? These are the same method (method #1). But since the method is virtual, then C has changed the implementation of this method by overriding it. – Yacoub Massad Dec 06 '15 at 19:16
  • 2) `var_b` does not know anything about method #2. It knows only about method #1. This is true because method #2 is defined on class `D`. Please note that although method #1 has been overridden in class `D`, its *definition* is in class `A`. This is why `var_b` knows about method #1. – Yacoub Massad Dec 06 '15 at 19:19