---
title: "What is a virtual method in C#? Also, give a suitable example."  
description: "What is a virtual method in C#? Also, give a suitable example."  
author: "Revati S Misra"  
published: 2023-04-14  
updated: 2023-06-15  
canonical: https://www.mindstick.com/forum/157796/what-is-a-virtual-method-in-c-sharp-also-give-a-suitable-example  
category: "c#"  
tags: ["c#", "oops"]  
reading_time: 2 minutes  

---

# What is a virtual method in C#? Also, give a suitable example.

What is a [virtual method](https://www.mindstick.com/interview/2600/can-you-override-private-virtual-method) in C#? Also, give a suitable example.

## Replies

### Reply by Aryan Kumar

A [virtual](https://yourviews.mindstick.com/view/81341/the-oh-so-fight-for-democracy-virtual-rally-scenes) [method in C#](https://www.mindstick.com/forum/33924/can-this-be-used-within-a-static-method-in-c-sharp) is a method that can be overridden in a derived class. When a method is declared as virtual in a base class, it allows a derived class to provide its own implementation of the method.

Here is an example of a virtual method in C#:

C#

```plaintext
public class Animal {
    public virtual void Speak() {
        Console.WriteLine("Generic animal sound");
    }
}

public class Dog : Animal {
    public override void Speak() {
        Console.WriteLine("Woof!");
    }
}

public class Cat : Animal {
    public override void Speak() {
        Console.WriteLine("Meow!");
    }
}
```

In the above example, the `Speak()` method is declared as virtual in the `Animal` class. This means that any derived class of `Animal` can override the `Speak()` method and provide its own implementation.

In the `Dog` and `Cat` classes, the `Speak()` method is overridden to provide a specific sound for each animal.

When an instance of `Dog` or `Cat` is created and the `Speak()` method is called, the implementation of the `Speak()` method in the appropriate class will be called.

Virtual methods are a powerful feature of C# that allow for polymorphism. Polymorphism is the ability to treat objects of different types in the same way. In the above example, the `Speak()` method can be called on an object of any type that inherits from `Animal`. The appropriate implementation of the `Speak()` method will be called, depending on the type of object that is used.

Virtual methods are often used in conjunction with interfaces. An interface is a collection of abstract methods. An abstract method is a method that does not have an implementation.

When a class implements an interface, it must provide an implementation for all of the abstract methods in the interface. This can be done by providing a concrete implementation of the method, or by overriding a virtual method in a base class.

Virtual methods and interfaces are powerful features of C# that allow for code reuse and flexibility. They can be used to create reusable code that can be used in a variety of situations.


---

Original Source: https://www.mindstick.com/forum/157796/what-is-a-virtual-method-in-c-sharp-also-give-a-suitable-example

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
