DEV Community

Hassan BOLAJRAF
Hassan BOLAJRAF

Posted on • Edited on

C# | Virtual Methods

Note
You can check other posts on my personal website: https://hbolajraf.net

In C#, a virtual method is a method that can be overridden in derived classes. This allows for polymorphism, where a base class reference can be used to invoke methods on a derived class object.

Syntax:

public class BaseClass
{
    public virtual void MyVirtualMethod()
    {
        // Base class implementation
    }
}

public class DerivedClass : BaseClass
{
    public override void MyVirtualMethod()
    {
        // Derived class implementation
    }
}
Enter fullscreen mode Exit fullscreen mode

Example:

Consider the following example:

using System;

public class Animal
{
    public virtual void MakeSound()
    {
        Console.WriteLine("Animal makes a generic sound");
    }
}

public class Dog : Animal
{
    public override void MakeSound()
    {
        Console.WriteLine("Dog barks");
    }
}

public class Cat : Animal
{
    public override void MakeSound()
    {
        Console.WriteLine("Cat meows");
    }
}

class Program
{
    static void Main()
    {
        Animal myDog = new Dog();
        Animal myCat = new Cat();

        myDog.MakeSound();  // Output: Dog barks
        myCat.MakeSound();  // Output: Cat meows
    }
}
Enter fullscreen mode Exit fullscreen mode

In this example, the Animal class has a virtual method MakeSound(). The Dog and Cat classes override this method with their own implementations. When instances of Dog and Cat are assigned to Animal references, the overridden methods are called based on the actual object type, demonstrating polymorphism.

What Next?

Virtual methods provide a way to implement and leverage the concept of dynamic method dispatch in object-oriented programming.

Top comments (4)

Collapse
 
jaywilkinson profile image
James Wilkinson

In the animal class wouldn't it be better to mark the MakeSound method abstract so any classes that derive from it must implement it

Collapse
 
hbolajraf profile image
Hassan BOLAJRAF • Edited

Yes, marking the MakeSound method as abstract in the Animal class would be a better approach if you intend for every derived class to provide its own specific implementation of the method. By doing so, you ensure that any subclass of Animal must override and provide a concrete implementation for MakeSound, enforcing a consistent behavior across all subclasses.

The implementation would be something like this:


public abstract class Animal
{
    public abstract void MakeSound();
}

public class Dog : Animal
{
    public override void MakeSound()
    {
        Console.WriteLine("Dog barks");
    }
}

public class Cat : Animal
{
    public override void MakeSound()
    {
        Console.WriteLine("Cat meows");
    }
}

class Program
{
    static void Main()
    {
        Animal myDog = new Dog();
        Animal myCat = new Cat();

        myDog.MakeSound();  // Output: Dog barks
        myCat.MakeSound();  // Output: Cat meows
    }
}
Enter fullscreen mode Exit fullscreen mode
Collapse
 
pauljlucas profile image
Paul J. Lucas

Would you please stop tagging these with #c?

Collapse
 
hbolajraf profile image
Hassan BOLAJRAF

I'm not tagging it as #c for fun, it was done by mistake. I have fixed a lot, so if I missed something, it's normalβ€”we are human. :)