简体   繁体   中英

How to force overrides in a derived class from non abstract base classes?

Is there any way for a derived class to be forced to override it's base classes' virtual method ?

The base class can't be abstract in my case so i can't use an abstract method. So i am wondering if this is even possible in C# ?

This is the general setup i am trying to do:

public abstract SomeAbstractClass {
   //Test() does not belong here.
}
public ClassA : SomeAbstractClass{    
   protected virtual void Test(){};    
}
public ClassB : ClassA{
   // how can i make this mandatory in the same way abstract methods work
   protected override void Test(){};
}

Is it possible at all ?

Is another intermediary class an acceptable solution? If so, you can override the virtual method as an abstract one, which would force inheritors to override.

The end result would look something like this:

public abstract class SomeAbstractClass { }

public class ClassA : SomeAbstractClass {
    protected virtual void Test() { }    
}

public abstract class ClassB : ClassA {
    protected override abstract void Test();
}

public class ClassC : ClassB {
    protected override void Test() { }
}

ClassC is forced to implement Test to inherit from ClassB , as Test is now abstract at this level of inheritence.

Is there any particular reason you couldn't use an Interface? This sounds like a good place for one since it doesn't define an implementation and therefore requires any class that implements the interface to define the method details.

I try to use composition over inheritance unless there is a very good reason to have an inheritance hierarchy.

// Whatever your top level abstract class is
public abstract class SomeAbstarctClass
{

}

// Interface that defines the signature of the Test method, but has no implementation detail.
// No need to define it as virtual here, since there is no implementation
public interface ITestMethodInterface
{
    void Test();
}

// Inherit from the absract class and implement the interface. This forces the new class to implement the interface, and therefore the Test method
public class ClassA : SomeAbstarctClass, ITestMethodInterface
{
    // This CAN, if needed be virtual, but I would recommend if it isn't absolutely needed for a hierarchy to simply implement it here and use the Interface in ClassB
    // to force the derviced class to implement it instead.
    public void Test()
    {
        // Class A's implementation of Test()
    }
}

// Here's where it might get complicated, if you MUST have a hierachy where Class B MUST inherit from Class A instead of SomeAbstractClass, then the implementation will carry over
// and it becomes difficult to FORCE the derviced class to override from ClassA
public class ClassB : SomeAbstarctClass, ITestMethodInterface
{
    public void Test()
    {
        // Class B's implementation of Test()
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM