How to Call the 'Base Implementation' of an Overridden Virtual Method

How can I call the 'base implementation' of an overridden virtual method?

Using the C# language constructs, you cannot explicitly call the base function from outside the scope of A or B. If you really need to do that, then there is a flaw in your design - i.e. that function shouldn't be virtual to begin with, or part of the base function should be extracted to a separate non-virtual function.

You can from inside B.X however call A.X

class B : A
{
override void X() {
base.X();
Console.WriteLine("y");
}
}

But that's something else.

As Sasha Truf points out in this answer, you can do it through IL.
You can probably also accomplish it through reflection, as mhand points out in the comments.

Requiring overridden virtual functions to call base implementations

The way its done is the base class method is not virtual and calls a protected virtual method.

Of course, that only handles one level.

In your particular situation, a large amount of infrastructure can make it work, but it's not worth it.

The typical response is to add a comment

// Always call base class method

Virtual method called from derived instead of base

The method implementation is chosen based on the execution-time type of the object. That's a large part of the point of it. Anyone can use:

public void Foo(Base b)
{
b.VirtualMethod();
}

... and not need to know or care what the execution type is, because polymorphism will take care of it.

I know i can call the Base's virtual method from the derived by calling base.VirtualMethod() but can I call it from outside?

No (at least, not without some horribly hackery to call the virtual method non-virtually), and that's a deliberate part of encapsulation. The overriding implementation has effectively replaced the original implementation for that object.

Allways call base.Method in Override without mentioning it for simple Scripts

As far as I know, there is no way to automatically invoke the base class's virtual method when an overridden one is invoked. You must explicitly call it.

One of the things you can do is break the parent method up a bit more. Instead of having all of the code in a single overridable method like this:

public class Foo
{
public virtual void Update()
{
// Do stuff
}
}

public class Bar : Foo
{
public override void Update()
{
// Replaces the parents implementation of the
// Update method due to not calling base.Load();
}
}

Instead, you can use the Template Method Pattern to break it up in to multiple parts, so that the user can override the part that is meant explicitly for them.

public class Foo
{
public void Update()
{
this.OnUpdating();
this.PerformUpdate();
this.OnUpdated();
}

public virtual void PerformUpdate()
{
// Leave this empty. Let the subclass override it and
// do their own thing. Your parent code will still
// get called when Update() is called.
}

public void OnUpdating()
{
// Invoke code that you want to guarantee is always
// executed PRIOR the overridden PerformUpdate() method
// is finished.
}

public void OnUpdated()
{
// Invoke code that you want to guarantee is always
// executed AFTER the overridden PerformUpdate() method
// is finished.
}
}

public class Bar : Foo
{
public override void PerformUpdate()
{
// Do custom stuff, don't have to call base.PerformUpdate()
// because it already does it's code in OnUpdating()
// and OnUpdated().
}
}

Hope this makes sense. This is what I do in my game engine. I then document that a call to base.PerformUpdate() is not needed. Another option is to make the PerformUpdate() method abstract, forcing children to implement it. That makes it a bit more clearer that there is no need to invoke base.PerformUpdate().

public class Foo
{
public void Update()
{
this.OnUpdating();
this.PerformUpdate();
this.OnUpdated();
}

// Child class is required to implement this method.
// Only downside is you will no longer be able to instance
// the base class. If that is acceptable, then this is really
// the preferred way IMO for what you are wanting to do.
public abstract void PerformUpdate();

public void OnUpdating()
{
// Invoke code that you want to guarantee is always
// executed PRIOR the overridden PerformUpdate() method is finished.
}

public void OnUpdated()
{
// Invoke code that you want to guarantee is always
// executed AFTER the overridden PerformUpdate() method is finished.
}
}

In the end, this approach lets your base class handle its update code safely, by forcing children to implement their own update method that you do not depend on. Your base class can run through its update stuff before and after the child-class has run through its updates.

This basically lets you do this in your game code:

Bar myFoo = new Bar();
myFoo.Update();

And you can rest assured that your base class update methods get called, and the child update code will get called as well.

Force base class virtual method call inside of base class

By marking your Info() method as virtual you are specifically asking for this type of inheritance behaviour to occur.

If you want to ensure that a method call in your base class is not overridden, you'll need to use a non-virtual method, e.g.

internal class BaseClass {
protected virtual void Info(){
this.FinalInfo();
}

protected void FinalInfo() {
Console.WriteLine("BaseClass");
}

internal virtual void CallInfo() {
this.FinalInfo();
}
}

how to implement virtual method of base class and also get the base method implementation in override method in c#

Overriding a virtual method does just that, overrides the default (base) implementation and replaces it with a new one. However, if you do not provide any overridden implementation for a virtual method, you automatically get the base implementation. So, the answer to your question is simply do not override the add method in the first place:

class B : A {}

However, if you need to keep the base implementation but wish to extend it, you can explicitly call the base implementation from a derived class, with the base keyword. For example:

class B : A
{
public override void add(int a, int b)
{
base.add(a, b);
DoSomethingElse();
}
}

Can I call a base class's virtual function if I'm overriding it?

In C++ you have to explicitly name the base class in calling the derived class method. This can be done from any method from the derived class. The override is a special case of the method of the same name. In Java there is no multi inheritance, so you can use super which will uniquely name the base class. The C++ syntax is like this:

class Bar : public Foo {
// ...

void printStuff() override { // help the compiler to check
Foo::printStuff(); // calls base class' function
}
};

How can I override a virtual method, but still invoke the base class version in C#

You can't on an override. Overrides replace the original (from the standpoint of the caller). An overridden method may call the base, but you can't externally.

Force method to call base method from base class

Although it is not possible to achieve this directly, you could get the same effect by writing your own method that is not virtual, which calls the virtual after performing some fixed operation:

public class A
{
public void Say()
{
Console.Write("A");
SayImpl();
}
protected virtual void SayImpl()
{
// Do not write anything here:
// for the base class the writing is done in Say()
}
}

public class B : A
{
protected override void SayImpl()
{
Console.Write("B");
}
}

Now any class inheriting from A and implementing SayImpl() would have A prepended to its printout.



Related Topics



Leave a reply



Submit