6

I know there are questions about calling the base method inside an overridden method such as this. But what if an overridden method only calls the base class? Is this bad/good deign? It just seems odd, why override a method just to call the base anyway?

For example see:

public class BaseClass
{
    virtual public void Method1()
    {
        //Do stuff
    }
}

public class InheritingClass : BaseClass
{
    override public void Method1()
    {
        base.Method1();
    }
}
  • 9
    It serves no purpose and just clutters up the code, so get rid of it. – David Arno Dec 15 '15 at 11:33
  • @DavidArno that's what I thought too, just making sure there wasn't a reason for it before I did so – TheLethalCoder Dec 15 '15 at 11:38
  • 2
    In an ideal world, the app will be covered by unit tests, thus you could remove it, and when the tests all still passed, you'd know it wasn't needed. We don't always live in that ideal world though, so it was sensible to ask :) – David Arno Dec 15 '15 at 13:39
  • The only reason I'd leave the code looking like the above would be if there was more code in the overridden method in the past, and it was removed, but could be viewed through old versions in the source control system. Leaving an artifact in the current code like this could signal to the dev today to view the file's history for the previous version of the method. – Graham Oct 29 '18 at 17:08
9

But what if an overridden method only calls the base class? Is this bad/good design?

Bad design? No, rather bad implementation. It's misleading for the maintenance programmer. When I don't see an override then I know the base is called. An override tells me there is something different, even if the base is called in there as well.


Requiring an override to call the base without a template is Bad Design

Referenced thread - most popular answser

My initial reaction to the Scandinavian override model is: CODING HORROR! It looks like I am forced to read all of that, and then more, to make sure the "sub-behavior" does not break my code and vice versa.

The Template Method Pattern is a good way to express variable code within some larger code flow and ensure correct execution order.

In my experience it so typical for a subclass to have to know what base-declared methods to call, and in order. Take a heaping handful of sub classes all with the same cut-n-paste control structure, add 5 years of maintenance; now you understand why I prefer 101 proof Wild Turkey.

P.S. All that is one big reason why I rail against the over-use of interfaces in lieu of abstract classes.

  • 2
    The over-use of interfaces in C# most likely comes from not wanting to shackle the implementer to that specific abstract base class. Using an interface allows the implementer to implement anther base class, and not just that specific abstract class. It depends a lot on the circumstances though. I find a good practice is to have both an IFoo interface and a FooBase class, which implements the IFoo interface, with abstract method-calls for the methods and auto-properties for the props. Maybe throw in a ctor as well. – die maus Dec 16 '15 at 21:31
0

IMO, virtual methods, in base class, have very basic implementation. Even if we override the definition of virtual method in child class, we can still call the virtual method (with basic implementation) while it makes sense and does not affect the intended behavior of the overridden method in the child class.

For example, BaseCalculator does the real work. Calculator is decorating the BaseCalculator library by extending it.

public class BaseCalculator
{
    public virtual T Add<T>(T number01, T number02)
    {
        return number01 + number02;
    }
}

public class Calculator: BaseCalculator
{
    public override T Add<T>(T number01, T number02)
    {
        Console.WriteLine(number01 + " | " + number02);
        var result = base.Add<int>(1, 3);
        Console.WriteLine("Result: "+ result);
        return result;
    }
}

Q: What if an overridden method only calls the base class?

A: In your case, having base class method as virtual, we need not to override it until we want to change its behavior.

Q: Is this bad/good deign?

A: Yes, it could bad design as it may clutter the source code file while we add unnecessary overrides.

Q: It just seems odd, why override a method just to call the base anyway?

A: BaseCalculator does the real work. Calculator is decorating the BaseCalculator library by extending it.

Hope, it helps!

Not the answer you're looking for? Browse other questions tagged or ask your own question.