Pages

24.5.11

Abstract vs. Virtual in C#

There are several keywords that can be used for inheritance in object-oriented programming languages. In C#, you can specify a method as abstract or virtual.

In the concrete class, you must override any method marked as abstract. You can override
virtual
methods. In addition, you can provide new implementation for normal methods that are neither
abstract nor virtual. In this case, it's recommended to use the new keyword.

What's the difference between abstract and virtual methods? How can I use them to serve my design? In this article, I'm trying to answer these questions.

Of course the abstract method doesn't have implementation. It just defines the method. But is there any other differences? Let's take an example of two classes. An abstract class and another one that inherits from it.
Class diagram of an abstract class and a concrete one


// the abstract class
public abstract class BaseClass
{
    public abstract string GetAbstractValue();

    public virtual string GetVirtualValue()
    {
        return "Base GetVirtualValue";
    }

    public string GetValue()
    {
        return "Base GetValue";
    }
}


// the concrete class
public class ConcreteClass : BaseClass
{
    public override string GetAbstractValue()
    {
        return "Concrete GetAbstractValue";
    }

    public override string GetVirtualValue()
    {
        return "Concrete GetVirtualValue";
    }

    public string GetValue()
    {
        return "Concrete GetValue";
    }
}

Let's make a console application that uses the above classes. We want to make 2 instances of the concrete class. The first one should be of type
BaseClass and the other one of type ConcreteClass.

The client code should look like this:

class Program
{
    static void Main(string[] args)
    {
        BaseClass instance1 = new ConcreteClass();
            
        Console.WriteLine(instance1.GetAbstractValue());
        Console.WriteLine(instance1.GetVirtualValue());
        Console.WriteLine(instance1.GetValue());


        ConcreteClass instance2 = new ConcreteClass();

        Console.WriteLine(instance2.GetAbstractValue());
        Console.WriteLine(instance2.GetVirtualValue());
        Console.WriteLine(instance2.GetValue());


        Console.Read();
    }
}

Let's now see and compare the results.

The results of running the application

What do you conclude from the results?

Let's make another experiment before conclusion. Let's remove the following 2 methods from the
ConcreteClass

  • GetVirtualValue
  • GetValue

Try to run the application and notice the results.

The results after removing the 2 methods

From the previous results we can conclude the following:


Abstract Virtual No Keyword
Can have implementation?  No Yes Yes
Can override? Must Can but not a must You can declare a new method with the same name
Which keyword to use to provide new implementation in the concrete class? override override No keyword needed
If an object is created of the base class type, which method will be executed? Concrete implementation The parent implementation will be called only if no implementation is provided in the concrete class Parent implementation
If an object is created of the concrete class type, which method will be executed? Concrete implementation Concrete implementation The parent implementation will be called only if no implementation is provided in the concrete class

Promotional Code for Udemy ServiceNow CIS - HR Practice Tests

If you're planning to become ServiceNow Certified Implementation Specialist - Human Resources (CIS-HR), you can prepare for the exam usi...