PositiveGuy
PositiveGuy

Reputation: 47743

Calling a base abstract method in an override of that method

If I have the following:

e.g.

public abstract class ClassA
{
     protected abstract void ValidateTransaction();
}

public abstract class ClassB : ClassA
{
     protected override void ValidateTransaction()
     {
          // some custom logic here
     }
}

public class ClassC : ClassB
{
     protected override void ValidateTransaction()
     {
          base.ValidateTransaction();
          // some additional custom logic here
     }
}

So I did not find usages on ClassC's ValidateTransaction. I don't see it being called anywhere.

So then I guess how does this work? I mean it's calling the method at the top of the stack here (calls the ClassB's override method and then includes logic in my ClassC's override of ClassB's method?)

This doesn't make sense to me why or how this works or the intention here.

UPDATED

Ok so I did find a spot where ClassA's PerformTransaction() method is called from a lot of sub classes in our project.

So ClassA now looks like this with more details for you here:

public abstract class ClassA
{
     public void PerformTransaction()
     {
           ValidateTransaction();
           // and calls some other code here.
     }

     protected abstract void ValidateTransaction();
}

Ok then we still have:

public abstract class ClassB : ClassA
{
     protected override void ValidateTransaction()
     {
          // some custom logic here
     }
}

public class ClassC : ClassB
{
     protected override void ValidateTransaction()
     {
          base.ValidateTransaction();
          // some additional custom logic here
     }
}


public class SomeAbritraryClass : ClassC
{
      ClassA.PerformTransaction();
      ...
 }

so ClassA.PerformTransaction() is being called in some classes that inherit ClassC.

Upvotes: 5

Views: 7086

Answers (3)

Reed Copsey
Reed Copsey

Reputation: 564383

So then I guess how does this work? I mean it's calling the method at the top of the stack here (calls the ClassB's override method and then includes logic in my ClassC's override of ClassB's method?)

Yes, that's basically what happens. When you call ValidateTransaction on an instance of ClassC, it's method is run. It, then, explicitly executes the base class (ClassB) method, then adds its own additional validation.

You're probably not finding direct usages of ClassB.ValidateTransaction() because there are no instances of ClassB (defined as ClassB) on which this is called. However, any ClassC call will be indirectly using ClassB.ValidateTransaction() through the base.ValidateTransaction() line.

Upvotes: 0

Jon Skeet
Jon Skeet

Reputation: 1500155

Well, it calls ClassC's override method... which happens to call ClassB's implementation. It's not "including" the logic of ClassB's implementation directly within the compiled code of ClassC, or anything like that - it's just another method call.

It's not entirely clear what's confusing you - the behaviour, the design intention, or what Find Usages is showing you.

Note that despite your subject line, you're not calling a "base abstract method" - you're calling the implementation of that method. The compiler knows that ClassC derives from ClassB, which is providing an implementation of the method, so it's making that call explicitly. You couldn't do the same thing from ClassB itself, because then base.ValidateTransaction really would be trying to call an abstract base method.

Fun fact: despite this calling a virtual method, it's a non-virtual method call: the compiler knows the exact implementation to use, and bakes that into the call. If it was a virtual call, you'd end up back in the ClassC implementation as that overrides it :)

Upvotes: 2

Tony The Lion
Tony The Lion

Reputation: 63190

When the C class override is called, it will first call into B's override and execute some logic there, which will then be extended by C's logic.

They could for all I know be working with the same variables, data. I don't know, cause I don't have all the data.

Upvotes: 0

Related Questions