C# Method Overriding

In c#, Method Overriding means override a base class method in derived class by creating a method with same name and signatures to perform a different task. The Method Overriding in c# can be achieved by using override & virtual keywords along with inheritance principle.

 

Suppose, if we want to change the behaviour of base class method in derived class, then we need to use method overriding. The base class method which we want to override in derived class that needs to be defined with virtual keyword and we need to use override keyword in derived class while defining the method with same name and parameters then only we can override the base class method in derived class.

 

In c#, the Method Overriding is also called as run time polymorphism or late binding. Following is the code snippet of implementing a method overriding in c# programming language.

 

// Base Class

public class Users

{

    public virtual void GetInfo()

    {

        Console.WriteLine("Base Class");

    }

}

// Derived Class

public class Details : Users

{

    public override void GetInfo()

    {

        Console.WriteLine("Derived Class");

    }

}

If you observe above code snippet, we created a two classes (“Users”, “Details”) and the derived class (Details) is inheriting the properties from base class (Users) and we are overriding the base class method GetInfo in derived class by creating a method with same name and parameters, this is called a method overriding in c#.

 

Here, we defined a GetInfo method with virtual keyword in base class to allow derived class to override that method using override keyword.

 

As discussed, only the methods with virtual keyword in base class are allowed to override in derived class using override keyword.

C# Method Overriding Example

Following is the example of implementing a method overriding in c# programming language.

 

using System;

 

namespace Tutlane

{

    // Base Class

    public class BClass

    {

        public virtual void GetInfo()

        {

            Console.WriteLine("Learn C# Tutorial");

        }

    }

    // Derived Class

    public class DClass : BClass

    {

        public override void GetInfo()

        {

            Console.WriteLine("Welcome to Tutlane");

        }

    }

    class Program

    {

        static void Main(string[] args)

        {

            DClass d = new DClass();

            d.GetInfo();

            BClass b = new BClass();

            b.GetInfo();

            Console.WriteLine("\nPress Any Key to Exit..");

            Console.ReadLine();

        }

    }

}

When we execute above c# program, we will get the result like as shown below.

 

C# Method Overriding Example Result

 

This is how we can achieve method overriding in c# to override a base class method in child class by defining a method with same name and signatures based on our requirements.

Difference between Overloading and Overriding

Following are the difference between method overloading and method overriding in c# programming language.

 

TypeDescription
Method Overloading Method Overloading means defining a multiple methods with same name but with different parameters.
Method Overriding Method Overriding means override a base class method in derived class by creating a method with same name and parameters using virtual and override keywords.