When to Use Abstract vs Virtual Method to Boost Efficiency


Abstract and virtual methods are two important methods for achieving the principles of object-oriented programming and method overriding for specific functionality implementations.

The abstract method is a method that is declared in an abstract class but is not implemented. The virtual method is declared in a base class and has an implementation, but the child class may override the default implementation.

This article explains what abstract and virtual methods are, providing C# code examples and how these method types compare with each other.

What is an abstract method?

Data abstraction is the process of hiding a method’s implementation details, exposing only essential information to the user. An abstract method is one way of achieving this.

An abstract method is a method with no implementation or method body and is declared using the ‘abstract’ keyword.

You can only declare abstract methods in abstract classes. Only the method signature will be included. An abstract method must be implemented in a non-abstract child class using the ‘override’ keyword.

Take a look at the following C# code example.

/ Abstract class
abstract class ParentClass
{
    // Abstract method
    public abstract int AbstractMethod(int x, int y);
}

// Derived class that inherits the Parent Class
class SubClass : ParentClass
{
    public override int AbstractMethod(int x, int y)
    {
        return x + y;
    }

    public static void Main()
    {
        var s = new SubClass();
        int total = s.AbstractMethod(10, 20);
        Console.WriteLine("total " + total);
    }
}

Output:

total 30

The abstract base class ParentClass declares the abstract method AbstractMethod without any method body. The SubClass that inherits the abstract class has now overridden the abstract method providing its implementation.

The compiler will provide an error if the sub-class does not override the abstract method.

'SubClass' does not implement inherited abstract member 'ParentClass.AbstractMethod(int, int).'

What is the virtual method?

A virtual method is a method declared using the ‘virtual’ keyword and can be overridden by sub-classes that inherit the class providing their own implementation.

Virtual methods have an implementation in both the parent and sub-classes. They are useful when you want to provide basic functionality, but sometimes, the sub-classes need to add more functionalities. This allows for polymorphism, where a derived class can be used in place of its base class.

Also, in an interface, methods are virtual and abstract by default.

The following example explains this concept.

public class Account
{
    //virtual method
    public virtual void CalculateInterest(double amount)
    {
        Console.WriteLine("Interest from base account {0}", amount * 0.1);
    }

    public static void Main()
    {
        Account account = new Account();
        Account savings = new Savings();
        Account current = new Current();

        account.CalculateInterest(1000.00);
        savings.CalculateInterest(1000.00);
        current.CalculateInterest(1000.00);
    }
}

// Child classes that inherit the Parent Class
class Savings : Account
{
    //overridden method
    public override void CalculateInterest(double amount)
    {
        Console.WriteLine("Interest from savings account {0}", (amount * 0.04) - 10.00);
    }
}

class Current : Account
{
    //overridden method
    public override void CalculateInterest(double amount)
    {
        Console.WriteLine("Interest from current account {0} ", amount * 0.01);
    }
}

Output:

Interest from base account 100
Interest from savings account 30
Interest from current account 10

The base class Account has the virtual method CalculateInterest. It has a default implementation. Child classes Savings and Current can now override the method and provide their own implementations.

Abstract methods vs virtual methods: a comparison table

Now, let’s see how these method types compare with each other.

Abstract MethodsVirtual Methods
An abstract method is a method that has no implementation or method body. Only the method signature is provided when declared in classes.A method that a child class can override and can have an implementation. You can keep the method body empty if it’s not required.
It can only be declared in abstract classes.
It can be used in abstract and non-abstract classes.
Use the ‘abstract’ keyword to declare a method as abstract.Use the ‘virtual’ keyword to declare a method as abstract.
The derived classes must always override the method and provide an implementation.The derived class does not always require overriding virtual methods (if they are declared in a non-abstract class), or it is optional to override a virtual method in the derived class.
Every abstract method is implicitly a virtual method.Explicitly virtual methods.
It can be used when classes are incomplete, or you do not want to provide any default implementation. But, it indicates to child classes that this functionality has to be implemented.It can be used when you require default implementations but need a different implementation in child classes.
Abstract methods allow information hiding or abstraction.Virtual methods allow dynamic polymorphism.
You cannot use the abstract modifier with the virtual, static, private, or override modifiers.You cannot use the virtual modifier with the abstract, static, private, or override modifiers.

Conclusion

Abstract and virtual methods are two ways to achieve method overriding in child classes. There are several differences from each other in the way they are declared and their uses.

The main difference is abstract methods do not need a method body, while virtual methods can provide a default implementation. Also, it is a must to override abstract methods in child classes, while it is optional for virtual methods.

The article provided some other ways they can differ from each other.

Recent Posts