C# inheritance
abstract-class, c#, inheritance, interface
Solution
Methods in C# are sealed by default. There is, however, nothing you can do to prevent method hiding (exposing a method with the same name in the derived class, commonly with `new`).
Or, for that matter, interface-reimplementation:
static void Main()
{
ISomeInterface si = new EvilClass();
si.DoSomething(); // mwahahah
}
public class EvilClass : ASomeAbstractImpl, ISomeInterface
{
public override void A() {}
public override void B() { }
void ISomeInterface.DoSomething()
{
Console.WriteLine("mwahahah");
}
}
Problem
Let's say I have the following code: ``` interface ISomeInterface { void DoSomething(); void A(); void B(); } public abstract class ASomeAbstractImpl : ISomeInterface { public abstract void A(); public abstract void B(); public void DoSomething() { // code here } } public class SomeImpl : ASomeAbstractImpl { public override void A() { // code } public override void B() { // code } } ``` The problem is that i wish to have the `ASomeAbstractImpl.DoSomething()` method sealed (final) so no other class could implement it. As the code is now `SomeImpl` could have a method called `DoSomething()` and that could be called (it would not override the method with the same name from the abstract class, because that's not marked as virtual), yet I would like to cut off the possibility of implementing such a method in `SomeImpl` class. Is this possible?