C# Method Hiding vs Overriding

C# Code Snippets Method Hiding vs Overriding
Share:

About

In this code snippet, we will see the difference between method hiding and overriding in C#.

The difference between method hiding and method overriding becomes evident when a class object is used polymorphically. The child version of the method gets called only when the method is overridden. If the method is hidden(new keyword) the parent version of the method gets called. Meanwhile, if we don’t call a method polymorphically the new keyword will act in the same way as the override keyword does and hide the original parent implementation of the method.

Let’s have a look at the code below to see how to hide a method.

Code:

using System;

namespace methodHiding
{
    class Program
    {
        static void Main(string[] args)
        {       
            MyChildClass MCC = new MyChildClass();

            //Parent class method was overriden.
            MCC.MyMethod();
            //Parent class method was hidden.
            MCC.MySecondMethod();

            //As you can see there is no difference between hiding and overriding if the class isn't used polymorphically.
            //The child version of the method gets called in both cases.

            //Polymorphism. 
            MyParentClass MPC = new MyChildClass();

            //Parent class method was overriden.
            MPC.MyMethod();
            //Parent class method was hidden.
            MPC.MySecondMethod();

            //As you can see when we use the class polymorphicly there is a difference between hiding and overriding.
            //The child version of the method gets called only when the method is overridden. If the method is hidden(new keyword) the parent version of the method gets called.

            Console.ReadLine();
        }
    }

    class MyParentClass
    {
        public virtual void MyMethod()
        {
            Console.WriteLine("Message from MyMethod.");
        }

        public virtual void MySecondMethod()
        {
            Console.WriteLine("Message from MySecondMethod.");
        }
    }

    class MyChildClass : MyParentClass
    {
        //This version of the method will override the original implementation from MyParentClass.
        public override void MyMethod()
        {
            Console.WriteLine("A little different message from MyMethod.");
        }

        //This version of the method will hide the original implementation from MyParentClass.
        public new void MySecondMethod()
        {
            Console.WriteLine("A little different message from MySecondMethod.");
        }
    }
}

Resulting output:

Share:

Leave a Reply

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

The following GDPR rules must be read and accepted:
This form collects your name, email and content so that we can keep track of the comments placed on the website. For more info check our privacy policy where you will get more info on where, how and why we store your data.

Advertisment ad adsense adlogger