C# virtual vs. abstract

When programming in C# on more or less large projects, sooner or later you will come across classes and methods that have a special modifier in front of them, such as the modifiers abstract and virtual. It is not immediately obvious what the difference is between abstract and virtual.

Abstract classes

An essential feature is that there can be abstract classes that can be preceded by the modifier abstract. However, there are no virtual classes. In addition to abstract, classes can also be preceded by the modifiers static, sealed and partial.

If you create an abstract class, you determine how it behaves in the context of inheritance. No instances can be created from an abstract class. The abstract class therefore only defines the basic framework for the properties to be inherited by a derived class.

For example, such a class could be called Animal, which defines the basic structure and from which all other animal classes, e.g. Bird, are then derived.

public abstract class Animal
{
    public string? Name { get; set; }
    public int Age { get; set; }
    public abstract void Eat();
    public abstract void Sleep();
}
C# Code example abstract class
public class Bird:Animal
{
    public override void Eat()
    {
        Debug.WriteLine("Bird Eat");
    }

    public override void Sleep()
    {
        Debug.WriteLine("Bird Sleep");
    }
    public Bird()
    {
        Name = "Bird";
        Age = 1;
    }
}
C# Code example derived class from abstract class

As you can see, abstract methods are also defined within the class. To avoid compiler errors, the methods defined as abstract must also be implemented in the derived classes. This means that abstract classes are very similar to Interfaces.

Abstract methods

In the code example above, we saw that methods can also be defined as abstract. Such abstract methods do not contain any code and must be overwritten in the derived class with override and implemented. See code example above, where we only make a console output with WriteLine as implementation.

The abstract methods are always part of an abstract class.

Virtual methods

The identifier virtual is always placed in front of a method if it can be overwritten by a derived class. The big difference to abstract is that a virtual method is implemented, i.e. it already contains code. In addition, a virtual method can be used in abstract classes as well as in normal classes. We illustrate this behaviour by adding the virtual method Move() to the code example above.

public abstract class Animal
{
    public string? Name { get; set; }
    public int Age { get; set; }
    public abstract void Eat();
    public abstract void Sleep();
    public virtual void Move()
    {
        Console.WriteLine("Animal Move");
    }
}
C# Code example with virtual method
public class Bird:Animal
{
    public override void Eat()
    {
        Debug.WriteLine("Bird Eat");
    }

    public override void Sleep()
    {
        Debug.WriteLine("Bird Sleep");
    }
    public Bird()
    {
        Name = "Bird";
        Age = 1;
    }
    public override void Move()
    {
        Debug.WriteLine("Bird is jumping");
    }
}
C# Code example derived class with override virtual method

The virtual method can be overwritten in the derived class, but does not have to be overwritten.

Differences between abstract and virtual summarized

  • abstract can be used as a modifier for classes; virtual cannot
  • abstract and virtual can be prepended to methods
  • abstract methods are part of abstract classes
  • virtual methods can be part of abstract and normal classes
  • abstract methods do not contain any code and must be overridden in the derived class
  • virtual methods contain code and can be overridden in derived classes

Leave a Reply

Your email address will not be published. Required fields are marked *