C # Inheritance

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 want the method to ASomeAbstractImpl.DoSomething()be sealed (final), so another class could not implement it. Since now the code SomeImplcan have a method called DoSomething(), and it can be called (it will not override the method with the same name from the abstract class, because it is not marked as virtual), but I would like to exclude the possibility of implementing such a method in the class SomeImpl.

Is it possible?

+5
source share
5 answers

# . , , ( , new).

, , -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");            
    }
}
+9

, .

# , , .

+2

SomeImpl DoSemthing, , , .

0

, , , , . , , , , virtual, , . . virtual, new.

0

All Articles