Virtual methods without body

18,742

Solution 1

As @Adam told you, there are many cases in which it makes sense. When you create an abstract class, it's because you want to create a common interface for all classes deriving from that one; however, at that level of inheritance you won't have enough information to be able to create working code for that method.

For example, if you create the class Figure, with the getArea() method, you won't be able to write code that is going to correctly calculate the area for all figures. You'll have to wait to write the code for Rectangle, or Circle (both deriving from Figure), in order to be able to write working code for them.

Solution 2

Because if the default behaviour is to do nothing, but derived classes might want to do something. It's a perfectly valid structure.

It allows your base code to call it. You tend to see similar designs when there is "BeforeXXX" and "AfterXXX" code, at the base class this code is empty, but the method needs to be there to compile. In derived classes, this code is optional, but needs to be virtual to be overridden.

The fact that it is in an abstract class shouldn't confuse its behaviour.

An example:

  abstract class Base
    {
        public void ProcessMessages(IMessage[] messages)
        {
            PreProcess(messages);

            // Process.

            PostProcess(messages);
        }

        public virtual void PreProcess(IMessage[] messages)
        {
            // Base class does nothing.
        }

        public virtual void PostProcess(IMessage[] messages)
        {
            // Base class does nothing.
        }
    }

    class Derived : Base
    {
        public override void PostProcess(IMessage[] messages)
        {
            // Do something, log or whatever.
        }

        // Don't want to bother with pre-process.
    }

If these methods (Pre, Post) were abstract, then all derived classes would need to implement them (likely as empty methods) - code litter that can be removed using empty virtual methods at the base.

Solution 3

If it is MANDATORY to override and no default logics could be written in base class, than virtuality is wrong and method should be abstract. If the default action is to do nothing, than as Adam mentioned, making empty virtual method in base class is perfectly valid structure

Solution 4

When you declare the method as abstract, the inherited class has to override that method (provide an implementation). It is mandatory.

When the method is declared as virtual, the inheritor can override the method and provide an implementation other than the default.

Solution 5

From a design perspective this smells bad and indicates that the implementation of the design is in an immature state. If a method is not required by every class that derives a particular base class then by definition it does not belong in the base class. You will usually discover that this method is used by particular derivations of the base class and that indicates a new interface or layer of abstraction in your inheritance hierarchy.

Share:
18,742

Related videos on Youtube

JeffreyZ.
Author by

JeffreyZ.

Updated on October 21, 2020

Comments

  • JeffreyZ.
    JeffreyZ. over 3 years

    I was looking at some code in an abstract class:

    public virtual void CountX(){}
    
    public virtual void DoCalculation() { ...code}
    

    Why should I declare an empty virtual method in an abstract class if it is not mandatory to override it in derived types?

  • Edurne Pascual
    Edurne Pascual about 13 years
    +1: you hit the spot describing the base implementation as the default behaviour. That's exactly the purpose of having virtual non-abstract methods. Even when the default is "do nothing", that's still a default ;)
  • Riegardt Steyn
    Riegardt Steyn almost 11 years
    Hmmm, so if I have a "do nothing" base method, should I call base.DoNothingMethod() from this.DoNothingMethod, or is it okay to omit that piece of code?
  • gretro
    gretro almost 10 years
    Although in this case, the method should be abstract. It does not make sens to leave it as virtual since you want all derived Shapes to calculate their Area. This behavior should not be optional and be left out.
  • Baltasarq
    Baltasarq almost 10 years
    Okay. If you have, say, twenty vehicles deriving from the base class Vehicle, and ten of them do not have a means to brake, but you need the brake() method, it is more comfortable to have it defined as virtual (not abstract), with an empty body, instead of being forced to override it to do nothing in all those classes.
  • ComeIn
    ComeIn over 9 years
    If you are going to down vote. Please at least provide a reason.
  • Kwex
    Kwex almost 9 years
    I agree with Comeln. DCastro's post here (stackoverflow.com/questions/22256157/…) sheds more light on this.
  • aaaaaa
    aaaaaa almost 7 years
    @Heliac - I had the same question and decided to omit the code following this advice
  • janv8000
    janv8000 over 3 years
    I would not be comfortable riding in a Vehicle where brake is an optional responsibility :s