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.
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 :)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With