Virtual methods are methods of a base class that can be overridden by a method in a derived class to provide a different implementation of that method. For example, you have method A from Class A and Class B inherits from Class A, then method A will be available from Class B. But method A is the exact method you inherited from Class A. What if you want to have a different behavior for the inherited method? Virtual methods solve this problem. Consider the following example.

using System;
 
namespace VirtualMethodsDemo
{
    class Parent
    {
        public virtual void ShowMessage()              
        {                                              
            Console.WriteLine("Message from Parent."); 
        }                                              
    }
 
    class Child : Parent
    {
        public override void ShowMessage()             
        {                                              
            Console.WriteLine("Message from Child.");  
        }                                              
    }
 
    class Program
    {
        public static void Main()
        {
            Parent myParent = new Parent();
            Child myChild = new Child();
 
            myParent.ShowMessage();
            myChild.ShowMessage();
        }
    }
}

Example 1 – Virtual Methods Example

Message from Parent.
Message from Child.

A virtual method can be defined by placing the virtual keyword in the declaration of the method. The virtual keyword indicates that the method can be overridden or, in other words, can have a different implementation. The class that inherits the Parent class contains the method that overrides the virtual method. You use the override keyword to indicate that a method should override a virtual method from the base class.

You can use the base keyword to call the original virtual method inside the overriding method.

using System;
 
namespace VirtualMethodsDemo2
{
    class Parent
    {
        private string name = "Parent";
 
        public virtual void ShowMessage()
        {
            Console.WriteLine("Message from Parent.");
        }
    }
 
    class Child : Parent
    {
        public override void ShowMessage()
        {
            base.ShowMessage();
            Console.WriteLine("Message from Child.");
        }
    }
 
    class Program
    {
        public static void Main()
        {
            Parent myParent = new Parent();
            Child myChild = new Child();
 
            myParent.ShowMessage();
            myChild.ShowMessage();
        }
    }
}

Example 2 – Using the base Keyword

Message from Parent.
Message from Parent.
Message from Child.

If you only use the base method and no other codes inside the overriding method, then it has a similar effect to not defining the overriding method at all. You cannot override a non-virtual method or a static method. The overriding method must also have similar access specifier as the virtual method. You can create another class that inherits the Child class and override its ShowMessage again and define a different implementation. If you want the overriding method to be final, that is, it cannot be overridden by the other classes that will derive from the class it belongs, you can use the sealed keyword.

public sealed override void ShowMessage()

Now if another class inherits from the Child class, it cannot override the ShowMessage() anymore.

Let’s have another example, we will override the ToString() method of the System.Object. Every class in C# including the ones you created inherits System.Object as you will see in the next lesson.

using System;
 
namespace VirtualMethodsDemo3
{
    class Person
    {
        public string FirstName { get; set; }
        public string LastName { get; set; }
 
        public override string ToString()      
        {                                      
            return FirstName + " " + LastName; 
        }                                      
    }
 
    class Program
    {
        public static void Main()
        {
            Person person1 = new Person();
 
            person1.FirstName = "John";
            person1.LastName = "Smith";
 
            Console.WriteLine(person1.ToString());
        }
    }
}

Example 3 – Overriding the ToString() Method

John Smith

Since we overridden the ToString() method of the System.Object class, we have customized the output to print the full name rather than printing the type of the object which is the default.

Written by compitionpoint

Leave a Comment

Your email address will not be published. Required fields are marked *