Search code examples
c#javainheritancepolymorphismsuper

Force base method call


Is there a construct in Java or C# that forces inheriting classes to call the base implementation? You can call super() or base() but is it possible to have it throw a compile-time error if it isn't called? That would be very convenient..

--edit--

I am mainly curious about overriding methods.


Solution

  • There isn't and shouldn't be anything to do that.

    The closest thing I can think of off hand if something like having this in the base class:

    public virtual void BeforeFoo(){}
    
    public void Foo()
    {
    
    this.BeforeFoo();
    //do some stuff
    this.AfterFoo();
    
    }
    
    public virtual void AfterFoo(){}
    

    And allow the inheriting class override BeforeFoo and/or AfterFoo