Lets say I have this:
class A { }
class B : A { }
class C : B { }
class Foo
{
public void Bar(A target) { /* Some code. */ }
}
class AdvancedFoo : Foo
{
public void Bar(B target)
{
base.Bar(target);
// Some code.
}
}
sealed class SuperiorFoo : AdvancedFoo
{
public void Bar(C target)
{
base.Bar(target);
// Some code.
}
}
Which overload will be called if I run new SuperiorFoo().Bar(new C()) and why?
I’m guessing it will be called cascadely but I can’t figure out why and if that behavior is guaranteed.
UPDATED
So, the base. works with both Foo and AdvancedFoo for SuperiorFoo, so which one will be called and why?
Edited my answer now that the question has been revised.
A quick trace shows the following:
Lets talk through what happens:
SuperiorFoo.Bar() calls its base method. Since SF.Bar() inherits
from AdvancedFoo, its base method is AdvancedFoo.Bar().
AdvancedFoo.Bar() then calls its base, which is Foo.Bar() since
AdvancedFoo inherits from Foo().
The process flow does NOT jump from SF.Bar() to Foo.Bar() because you could potentially want behaviour from the intermediate class.
If we remove the method from AdvancedFoo, the traversal is slightly different. SuperFoo.Bar() will still call its base method, but since AdvancedFoo doesn’t hide the Foo.Bar() method anymore, the logic will jump to the Foo.Bar() method.