Tech Point Fundamentals

Tuesday, November 17, 2020

Virtual Methods and Method Overriding C#

 Virtual Methods and Method Overriding C#


In C# virtual methods are used for achieving polymorphism. The term "polymorphism" is a Greek word which means "having multiple forms". The "compile-time polymorphism" is achieved by "method overloading" and operator overloading while "run time polymorphism" is achieved by "method overriding". In this article, we will learn about virtual methods and overriding virtual methods in detail.


Virtual methods are used to implement dynamic binding. It is also known as late binding or dynamic polymorphism. The virtual keyword can be used to modify a method, property, indexer, or event declaration which allows it to be overridden in a derived class.

Virtual Method

A method modified by the "virtual" keyword is known as a virtual method. The virtual method may be implemented differently in different inherited classes, and the call to these methods are decided at runtime.

When a virtual method is invoked, the run-time type of the object is checked for an overriding member. The overriding method in the most derived class is called, which might be the original method if no derived class has overridden the virtual method.

By default, all the methods are non-virtual, so you cannot override a non-virtual method. With the introduction of C# 8, interfaces can also have virtual methods.

Please read more about the interface virtual method here.

Virtual Method Fundamental Points

    1. The "virtual" keyword must be used to make a method virtual because all the methods are non-virtual by default.
    2. The "virtual" keyword must be used before the return type of the method.
    3. The access modifier of the virtual method should be the same in both the base class and its derived class.
    4. Virtual methods can be overloaded in C#.
    5. Virtual methods can have both out and ref type parameters.
    6. If the derived class also has a method with the same name and signature as the base class virtual method, then the base class method will be hidden in the derived class.
    7. A virtual method can be overridden to sealed also in the derived class.

Virtual Method Fundamental Rules

    1. A non-virtual method cannot be overridden and by default, all the class methods are non-virtual in C#.
    2. Virtual methods can be overridden in the derived class, but it is not mandatory like abstract methods.
    3. Virtual methods cannot be private in C#.
    4. All the virtual methods must have to provide the body definition in C#.
    5. A virtual method cannot be static or abstract in C#.
    6. The virtual modifier cannot be used when overriding the virtual method i.e. virtual modifiers cannot be used with the override keyword.
    7. A constructor or destructor cannot be virtual in C#. If you remember in C++ virtual destructor is allowed.

Why Virtual Methods

    1. Virtual methods are used to achieve run time polymorphism in C#.
    2. Normally, a virtual method is used in the base class when there is a common functionality for all the derived classes, but some derive classes may need some different functionality.



Method Overriding

Method overriding is an example of dynamic polymorphism in C#.  Method overriding is a feature that allows a child class to provide a specific implementation of a base class method.

The method which is overridden by an override declaration in the derived class is known as the "overridden base method". This override method is a new implementation of the method that is inherited from the base class.

A virtual method can be overridden in the derived class using the "override" keyword. The "virtual" modifier cannot be used with the "override" keyword. Also, the access modifier must be the same as the base class virtual method.

Virtual Method Overriding Vs Abstract Method Overriding

    1. Overriding the virtual method is optional while overriding the abstract methods is mandatory.
    2. It is an error to use the "virtual" modifier with the "override" keyword but the "abstract" modifier can be used with the "override" keyword. 
    3. Abstract methods can only be defined in an abstract class while virtual methods can be defined in non-abstract classes as well.

Please read more about the abstract method here.

Virtual Property

A property can also be declared as virtual in C#. Virtual properties behave just like virtual methods, except for the differences in declaration and invocation syntax.

The virtual property can be overridden in a derived class by including a property declaration that uses the override modifier.

Real Examples of Virtual Methods 

    1. System.Type abstract class contains a lot of virtual methods and properties like GetArrayRank(), GetEnumName(), GetEvents() etc.
    2. System.Reflection.Assembly is an abstract class that contains a lot of virtual methods and properties like GetType(), GetSatelliteAssembly(), LoadModule() etc.
    3. System.Object is a concrete class which contains Equals(), GetHashCode(), ToString() virtual methods.
    4. System.Globalization.CultureInfo is a concrete class which contains Name, Calendar, DateTimeFormat, DisplayName etc virtual properties and Clone() virtual method.

Live Demo


No comments:

Post a Comment

Please do not enter any HTML. JavaScript or spam link in the comment box.