Let's say I have these classes:
abstract class A
{
protected abstract void DoSomething();
protected abstract void DoSomethingAnotherName();
}
abstract class SuperA : A
{
private new void DoSomething()
{
base.DoSomething(); // <-- I can't do this.
DoSomethingAnotherName(); // <-- But I can do this.
}
}
sealed class FinalA : SuperA
{
protected override void DoSomething() { ... }
protected override void DoSomethingAnotherName() { ... }
}
Can I call base class abstract method from another abstract class specifying base
keyword without casting? I can always rename private new DoSomething
method and remove base.
part, then it works. But what if I want this method to be named like this?
((dynamic)this).DoSomething() /* or */ ((FinalA)this).DoSomething()
also doesn't work since at runtime this line runs inside the SuperA
class and it can see it's own private DoSomething
method, so there's StackOverflowException
.
P.S. I don't really need it, I just noticed that I have no new private methods in my derived class. My code is like with DoSomethingAnotherName
method. I just got curious would it be possible to run this code if there actually was a method with the same name in a derived class. And it seems like it's not possible.
If anyone feels like describing what CLR does and how it is all compiled to IL and why it can't be possible at all - I'll appreciate this, I'm into the topic. To me it seems strange though that abstract class declares an "interface" of abstract methods and we can't call it from derived types. I understand that we can't create abstract classes since they are not implemented yet. But when I use base from derived class - that's obvious that I run an instance that already has been constructed somewhere...