1

I can invoke the parent class method by using base.virtualParentMethod(). But how do I call the method in parent-parent class without creating an object of it, in the following scenario.

 class A
    {
        public virtual void virtualParentMethod()
        {
            Console.WriteLine("A");
        }
    }
    class B : A
    {
        public override void virtualParentMethod()
        {
            Console.WriteLine("B");
        }
    }
    class C : B
    {
        public override void virtualParentMethod()
        {
            //base.virtualParentMethod();
            //This is where I want to invoke the method of A
            //So that out Will be : A
        }
    }
Simsons
  • 12,295
  • 42
  • 153
  • 269
  • If you need to do this, the odds are you need to refactor this. You seem to have a mismatch between your inheritance model and your desired behaviour – Pete Apr 16 '12 at 12:13
  • duplicate - http://stackoverflow.com/questions/438939/is-there-any-way-to-call-the-parent-version-of-an-overridden-method-c-net – scibuff Apr 16 '12 at 12:14
  • If you really want to do this, perhaps you should consider changing your design... – Torbjörn Kalin Apr 16 '12 at 12:15

5 Answers5

4

If you need some parent functionality in not direct children of parent, then you should move that functionality to separate method:

class A
{
    public virtual void VirtualParentMethod()
    {
        Foo();
    }

    protected void Foo()
    {
        Console.WriteLine("A");
    }
}
class B : A
{
    public override void VirtualParentMethod()
    {
        Console.WriteLine("B");
    }
}
class C : B
{
    public override void VirtualParentMethod()
    {        
        Foo();
    }
}

UPDATE

Also consider:

  • inheriting C directly from A
  • changing inheritance order, i.e. B from C
  • extracting this behavior to other object
Sergey Berezovskiy
  • 232,247
  • 41
  • 429
  • 459
2

You cannot pick what version of a derived method to run using the base syntax. The only way to run a specific method would be to instantiate a version of A inside C that can be used for that method (basically, a real instance of A).

However I personally wouldn't do this. This is indicative of a design issue with your inheritance chain.

If you need to ensure that C runs A, why not inherit A from C - C : A.

Adam Houldsworth
  • 63,413
  • 11
  • 150
  • 187
0

You simply can not, cause that is an essence of Virtual Method Table, so virtual keyword and its override. Invokation method's address will be lookuped from corresponding real type Virtual Methods Table.

Tigran
  • 61,654
  • 8
  • 86
  • 123
0

You can't. using base.MethodName will always call the closest implementation up the hierachy and there's no way you can by pass this without explicitly declaring a method that can be called

it's also worth noting that if you find yourself in a situation where you wish to do this odds are you have a design flaw.

Rune FS
  • 21,497
  • 7
  • 62
  • 96
0

You've already created an instance of A, by creating an instance of C (as C : B, B : A), so there is a way you can do this with a small modification. Now, without indicating whether or not this is a good idea or not;

If you use the 'new' keyword instead of override on your derived method implementations, then you can call the base implementation of a method directly, bypassing the intermediate class implementation, by casting the object to the type who's implementation you want to use, like so;

class A
{
    public virtual void virtualParentMethod()
    {
        Console.WriteLine("A");
    }
}
class B : A
{
    public new void virtualParentMethod()
    {
        Console.WriteLine("B");
    }
}
class C : B
{
    public new void virtualParentMethod()
    {
        // casting this to A will allow you to call the base class implementation
        ((A)this).virtualParentMethod();
    }
}

Note that if you make this change, you've introduced a behavioural change to any callers of the method, depending on how they refer to the object. And if you try to cast in this way in the existing C.virtualParentMethod implementation (declared 'override'), you are really just calling the method itself and will get in an infinite loop.

Or, you could just reconsider your class design. :-)

RJ Lohan
  • 6,497
  • 3
  • 34
  • 54