Shakti Prakash Singh
Shakti Prakash Singh

Reputation: 2533

Access Parent Class virtual method from inheriting Child Class Object

I would like to know if it is possible to access the base virtual method using a inheriting class (which overrides the method) object.

I know this is not a good practice but the reason I want to know this is if it is technically possible. I don't follow such practice, asking just out of curiosity.

I did see a few similar questions but I did not get the answer I am looking for.

Example:

public class Parent
{
    public virtual void Print()
    {
        Console.WriteLine("Print in Parent");
    }
}

public class Child : Parent
{
    public override void Print()
    {
        Console.WriteLine("Print in Child");
    }
}

class Program
{
    static void Main(string[] args)
    {
         Child c = new Child();
         //or Parent child = new Child(); 
         child.Print();  //Calls Child class method
         ((Parent)c).Print(); //Want Parent class method call
    }
}

Upvotes: 5

Views: 4849

Answers (4)

Jesse C. Slicer
Jesse C. Slicer

Reputation: 20157

As per the linked duplicate I commented with, you can do it with some reflection tricks as such:

static void Main(string[] args)
{
    Child child = new Child();
    Action parentPrint = (Action)Activator.CreateInstance(typeof(Action), child, typeof(Parent).GetMethod("Print").MethodHandle.GetFunctionPointer());

    parentPrint.Invoke();
}

Upvotes: 4

bebopayan
bebopayan

Reputation: 45

I wouldn't know when this would be helpful. But a decent workaround could be to either overload or write a dummy method that only calls the father class. It would look something like this:

public class Child : Parent
{
    public void Print(bool onlyCallFather)
    {
    if(onlyCallFather)
        base.Print();
    else
        Print();
    }
}

And then in your main method:

class Program
{
    static void Main(string[] args)
    {
         Child c = new Child();
         child.Print(false);  //Calls Child class method
         child.Print(true);  //Calls only the one at father
    }
}

So it would do what you wanted to do. I've actually seen this type of workaround to tell if you want the base method to be called or not.

Upvotes: 0

mihirj
mihirj

Reputation: 1219

According to me, the best you can do is:

public class Parent
{
    public virtual void Print()
    {
        Console.WriteLine("Print in Parent");
    }
}

public class Child : Parent
{
    public override void Print()
    {
        base.Print();
        Console.WriteLine("Print in Child");
    }
}

class Program
{
    static void Main(string[] args)
    {
         Child c = new Child();
         //or Parent child = new Child(); 
         child.Print();  //Calls Child class method
         ((Parent)c).Print(); //Want Parent class method call
    }
}

Upvotes: 1

Mayur Manani
Mayur Manani

Reputation: 825

Nope - it is not possible to invoke the Virtual method of Base class - The most derived implementation of the method is invoked in such scenarios. In the example given by you, it would print "Print in Child" in both the cases.

Upvotes: 1

Related Questions