Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calling a base abstract method in an override of that method

Tags:

c#

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.

like image 228
PositiveGuy Avatar asked Nov 05 '22 23:11

PositiveGuy


1 Answers

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 :)

like image 101
Jon Skeet Avatar answered Nov 12 '22 22:11

Jon Skeet