Coding For Noobs

Method Overriding, Virtual Method in Dynamic Polymorphism

Method Overriding and Virtual Method are the types of methods in Dynamic Polymorphism. Both Method Overriding and Virtual Methods are related to each other, They can’t be used without each other. Why is that so? you might ask. I will explain further in this article with a program example. First, we need to understand why we use these methods, and then you will be able to explain them nicely.

Why Method Overriding Concept

First, we need to understand, in what situation we need to use method overriding. Suppose you have two classes called Class A and Class B, this is called Inheritance. Class A is the base class (Parent Class) of Class B. Both Class A and Class B have a method with the same name and same signature. We do this trick to make a Class multi-functional. We give different implementations to both child and parent class methods and use these different implementations to our advantage.

This trick gives us the power to use a single class function with two completely different implementations. Polymorphism is a concept that is used a lot in the real world. There are hundreds of games you have seen where polymorphism is used, if I say a game can not be created if without using polymorphism, It will not be false.

Override Keyword

Whenever a method is declared as an Override method, it means there is a method in the parent class with the same name and same signature. This Override method is written to provide implementation to the parent class method.

Virtual Keyword

Whenever a method is declared as a Virtual method, it means this Virtual method is just a dummy method, This virtual method will be overridden by a child method. The Virtual keyword gives permission to the child class Override method to provide implementation to this Virtual method. Whenever the program runs, a Virtual method waits for the Override Method in the child class to provide its implementation.

Example

I play Counter-Strike, so I will give you an example of that. In counter-strike when the game starts, the player has a normal gun. Later on, when the player purchases a gun the shooting capability changes, there are several types of guns that are available for the player to purchase like M4 A1, AK47, M4S, AWP etcetera. If the play purchases M4 A1 the gun starts shooting like M4 A1. When the player purchases an AK47 the functionality of the gun changes again. All of this is possible due to Polymorphism.

Look at the program below for the above example.

using System;

namespace Polymorphism
{
    class Gun
    {
        public virtual void Shoot()
        {
            Console.WriteLine("The gun shooting like a normal Pistak");
        }
    }

    class M4 : Gun
    {
        public override void Shoot()
        {
            Console.WriteLine("The gun is shooting as M4");
        }
    }

    class AK47 : Gun
    {
        public override void Shoot()
        {
            Console.WriteLine("The gun is shooting as AK47");
        }
    }

    class AWP : Gun
    {
        public override void Shoot()
        {
            Console.WriteLine("The gun is shooting as AWP");
        }
    }


    class Program
    {
        static void Main(string[] args)
        {
            Gun gun = new M4();

            gun.Shoot();

            gun = new AK47();

            gun.Shoot();

            gun = new AWP();

            gun.Shoot();

            Console.ReadLine();
        }
    }
}

Output

Explanation

There is a class called Gun. Gun class has a method called Shoot. The Shoot method in Gun class is a Virtual Method. There are some other classes like M4, AK47, and AWP. All of those classes have the same Shoot methods. The methods inside M4, AK47, and AWP are the override methods. The Override keyword is telling us that these Shoot methods are written to provide implementation to parent class Virtual method.

I hope the use of Virtual and Override keywords is now clear for you. In case of any confusion, please feel free to comment, I will be glad to assist you.

Till then, Stay Safe Stay Healthy!

Exit mobile version