---
title: "What is the difference between the Virtual method and the Abstract method?"  
description: "What is the difference between the Virtual method and the Abstract method?"  
author: "Rahul Roi"  
published: 2020-11-16  
updated: 2023-06-06  
canonical: https://www.mindstick.com/interview/33757/what-is-the-difference-between-the-virtual-method-and-the-abstract-method  
category: "c#"  
tags: ["c#", ".net"]  
reading_time: 4 minutes  

---

# What is the difference between the Virtual method and the Abstract method?

Where the Virtual method must always have a default implementation. Although, this can be overridden in the derived class, however, it is not mandatory. This can be overridden using the override keyword.

Where an Abstract method does not have any implementation. This resides in the abstract class. This is mandatory that the derived class implements the abstract method. The override keyword is not necessary here though it can be used.

## Answers

### Answer by Aryan Kumar

Virtual and abstract methods are both concepts in object-oriented programming (OOP) that allow for code to be reused and for polymorphism to be implemented. However, there are some key differences between the two.

A virtual method is a method that can be overridden by a subclass. This means that the subclass can provide its own implementation of the method. A virtual method is declared using the virtual keyword. For example, the following code declares a virtual method called Bar() in the Foo class:

Code snippet

```plaintext
class Foo
{
    virtual void Bar() {}
}
```

A subclass can override the Bar() method by simply declaring a method with the same name and signature. For example, the following code declares a subclass of Foo called Bar that overrides the Bar() method:

Code snippet

```plaintext
class Bar : Foo
{
    public override void Bar()
    {
        // implementation of Bar()
    }
}
```

When an object of type Bar is created and the Bar() method is called, the implementation of Bar() in the Bar class will be called.

An abstract method is a method that cannot be overridden by a subclass. This means that the subclass must provide its own implementation of the method, or it will not compile. An abstract method is declared using the abstract keyword. For example, the following code declares an abstract method called Baz() in the Foo class:

Code snippet

```plaintext
abstract class Foo
{
    abstract void Baz();
}
```

A subclass of Foo cannot simply declare a method with the same name and signature as Baz(). The subclass must also provide its own implementation of Baz(). For example, the following code declares a subclass of Foo called Baz that provides an implementation of the Baz() method:

Code snippet

```plaintext
class Baz : Foo
{
    public override void Baz()
    {
        // implementation of Baz()
    }
}
```

When an object of type Baz is created and the Baz() method is called, the implementation of Baz() in the Baz class will be called.

The main difference between virtual and abstract methods is that virtual methods can be overridden by subclasses, while abstract methods cannot. This means that virtual methods provide more flexibility, as subclasses can choose to implement the method in their own way. Abstract methods, on the other hand, force subclasses to provide an implementation of the method, which can make code more rigid.

Here is a table that summarizes the key differences between virtual and abstract methods:

| Feature | Virtual Method | Abstract Method |
| --- | --- | --- |
| Can be overridden | Yes | No |
| Flexibility | More flexible | Less flexible |
| Use cases | Used when the implementation of a method can vary between subclasses | Used when the implementation of a method must be provided by a subclass |

Ultimately, the best type of method to use will depend on the specific requirements of the situation. If the implementation of a method can vary between subclasses, then a virtual method should be used. If the implementation of a method must be provided by a subclass, then an abstract method should be used.

### Answer by Rahul Roi

Where the Virtual method must always have a default implementation. Although, this can be overridden in the derived class, however, it is not mandatory. This can be overridden using the override keyword.

Where an Abstract method does not have any implementation. This resides in the abstract class. This is mandatory that the derived class implements the abstract method. The override keyword is not necessary here though it can be used.


---

Original Source: https://www.mindstick.com/interview/33757/what-is-the-difference-between-the-virtual-method-and-the-abstract-method

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
