0

I am trying to call a Super base class method from a derived class

public class Base
{
    public virtual void TestFun()
    {
        Console.WriteLine("This is Class Base");
    }
}

public class A : Base
{
    public override void TestFun()
    {
        Console.WriteLine("This is Class A");

        base.TestFun();
    }
}

public class B : A
{
    public override void TestFun()
    {
        Console.WriteLine("This is Class B");

        base.TestFun();
    }
}

public class Program
{
    public static void Main()
    {
        Base obj = new B();

        obj.TestFun();
    }
}

I am getting the output as

  1. This is Class B
  2. This is Class A
  3. This is Class Base

I want to know how can we skip calling the function in A. That means I want to call class "Base" function from class "B"

Zohar Peled
  • 79,642
  • 10
  • 69
  • 121
  • 2
    Does this answer your question? [How to call base.base.method()?](https://stackoverflow.com/questions/2323401/how-to-call-base-base-method) – asaf92 Aug 24 '20 at 11:59
  • @asaf92, Thank you for the response. I cannot alter the code in second layer (ie class A). do we have anyother provision ? – Hari Sankar Aug 24 '20 at 12:15
  • Calling a superbase class is not a good practice. Can you please let us know why do you want to do that? – Sowmyadhar Gourishetty Aug 24 '20 at 12:18
  • 2
    _"I want to know how can we skip calling the function in A. That means I want to call class "Base" function from class "B""_ If you have the urge to do that, then your inheritance tree is wrong. Given the comment you cannot change `A`, you probably need to extend `Base` directly. – Fildor Aug 24 '20 at 12:19
  • 1
    TL;DR; You can, but you really shouldn't. – Zohar Peled Aug 24 '20 at 12:28
  • @HariSankar Yes, refactor your code – asaf92 Aug 24 '20 at 13:00

0 Answers0