Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calling the grand-parent implementation of an overridden method [duplicate]

Tags:

c#

inheritance

Possible Duplicate:
How to call base.base.method()?

I have some trouble with Inheritance in C#. I've sketched three classes: A, B and C. C inherits from B and B from A. Now the B class calls base.Method1 and works fine but I can't call A.method1 from the C class. If I call base.Method1 from C obviously that method will be method1 of B. Any advice?

P.S. in A class there are some fields marked private so you can access them only

class A
{    
    private instance;    
    public virtual void Method1 ()
    {       
        instance = this;
        do something;       
    }
}

class B : A
{
    public override void Method1()
    {
        base.Method1();
        do something;       
    }
}

class C : B
{   
    public override void Method1 ()
    {
        //need A Method1 then do something
    }
}
like image 913
Pasquale Sada Avatar asked Aug 21 '12 09:08

Pasquale Sada


People also ask

How do you call an overridden method?

But, since you have overridden the parent method how can you still call it? You can use super. method() to force the parent's method to be called.

How do you call a grand parent in Java?

In Java, a class cannot directly access the grandparent's members. It is allowed in C++ though. In C++, we can use scope resolution operator (::) to access any ancestor's member in the inheritance hierarchy. In Java, we can access grandparent's members only through the parent class.

How to call overridden method in Python?

In Python method overriding occurs by simply defining in the child class a method with the same name of a method in the parent class. When you define a method in the object you make this latter able to satisfy that method call, so the implementations of its ancestors do not come in play.

Can we override already overridden method?

You cannot override a non-virtual or static method. The overridden base method must be virtual , abstract , or override . An override declaration cannot change the accessibility of the virtual method. Both the override method and the virtual method must have the same access level modifier.


2 Answers

This is a feature of C#. If you wish to expose this method to class C, consider refactoring A like so:

class A
{    
  private instance;    
  public virtual void Method1 ()
  {
    AMethod1();       
  }

  protected void AMethod1()
  {
    instance = this;
    do something;       
  }
}

This will enable you to call this method from within C:

class C : B
{   
  public override void Method1 ()
  {
    AMethod1();
    // do something;
  }
}
like image 193
Rich O'Kelly Avatar answered Oct 13 '22 13:10

Rich O'Kelly


It is not possible to do this in C#, thought its possible to do this via IL.

For your case, you may do something like this:

class A
{
    private int instance;
    public virtual void Method1()
    {
        Method1Impl();
    }

    protected void Method1Impl()
    {
    }
}

Now you can call A.Method1Impl from your C class.

like image 22
logicnp Avatar answered Oct 13 '22 14:10

logicnp