9

I tested code like this:

class A
{
    public A() { }

    public virtual void Test ()
    {
        Console.WriteLine("I am A!");
    }
}

class B : A
{
    public B() { }

    public override void Test()
    {
        Console.WriteLine("I am B!");
        base.Test();
    }
}

class C : B
{
    public C() { }

    public override void Test()
    {
        Console.WriteLine("I am C!");
        base.base.test(); //I want to display here "I am A"
    }
}

And tried to call from C method Test of A class (grandparent's method). But It doesn't work. Please, tell me a way to call a grandparent virtual method.

Tadeusz
  • 6,453
  • 9
  • 40
  • 58
  • 1
    Grandparent's methods are not accessible. you'll have to set something up at B level that accesses A and call that from C. – Bala R Oct 04 '11 at 13:50
  • 1
    base.base.test(); <-- is this just a typo? your method begins with a capital T. – rie819 Oct 04 '11 at 13:50
  • doesn't calling just base.test() in C get you what you want ? – ashutosh raina Oct 04 '11 at 13:50
  • 2
    What is your actual problem that is driving you to want to do this? We might be able to help you solve *that* problem. – Anthony Pegram Oct 04 '11 at 13:50
  • @ashutoshraina, calling `base.Test()` in C invokes B, which then invokes A. The user wants to invoke A directly. – Anthony Pegram Oct 04 '11 at 13:51
  • My interest in that is only research. I think there is people on this forum, who knows why C# developers decided to do so. – Tadeusz Oct 04 '11 at 13:54
  • Its not possible with base. In fact if you need something like this, i would reconsider the class/method design. You can of course use a flag, call another method etc. But just using base it is not possible. Some say that ((A)C_Instance).Test would work, well you can try but i can't confirm that this will work, because i try to modify my design if this would be necessary. – dowhilefor Oct 04 '11 at 13:56
  • 5
    I answer your question here: http://blogs.msdn.com/b/ericlippert/archive/2010/12/13/all-your-base-do-not-belong-to-you.aspx – Eric Lippert Oct 04 '11 at 14:45
  • It is good that C# is more restricted than even CLR. It seems we need to migrate on C++.net or something else :( – Tadeusz Oct 05 '11 at 11:18

2 Answers2

14

You can't - because it would violate encapsulation. If class B wants to enforce some sort of invariant (or whatever) on Test it would be pretty grim if class C could just bypass it.

If you find yourself wanting this, you should question your design - perhaps at least one of your inheritance relationships is inappropriate? (I personally try to favour composition over inheritance to start with, but that's a separate discussion.)

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
5

One option is to define a new method in B as shown below

class B : A
{
    public B() { }

    public override void Test()
    {
        Console.WriteLine("I am B!");
        base.Test();
    }

    protected void TestFromA()
    {
        base.Test()
    }
}

and use TestFromA() in C

Bala R
  • 107,317
  • 23
  • 199
  • 210