Method Hiding in C#

Method Hiding in C# with Examples

In this article, I am going to discuss Method Hiding in C# with Examples. Please read our previous article, where we discussed Method Overriding in C# with examples. At the end of this article, you will understand what exactly Method Hiding is and when and how to use Method Hiding in C#?

What is Method Hiding in C#?

When we use the new keyword to hide a base class member, then it is called Method Hiding in C#. We will get a compiler warning if we miss the new keyword. This is also used for re-implementing a parent class method under child class. Re-implementing parent class methods under child classes can be done using two different approaches, such as

  1. Method Overriding
  2. Method Hiding

In the first case, we re-implement the parent class methods under child classes with the permission of parent class because here in parent class the method is declared as virtual giving permission to the child classes for overriding the methods using the override modifier.

In the 2nd approach, we re-implement the method of parent class even if those methods are not declared as virtual that is without parent permission we are re-implementing the methods.

The syntax for Method Hiding in C#:

Please have a look at the following image to understand the syntax of Function Hiding in C#.

Method Hiding in C# with Examples

Note: Using the new keyword for re-implementing the methods in the child class is optional and if used will give information to hiding.

Example to understand Method Hiding in C#:

Please have a look at the following example to understand the concept of Method Hiding in C#. As you can see, in the parent class we have not used the virtual keyword in the method signature of the display method. In the child class, we have re-implement the method using the new keyword.

namespace PolymorphismDemo
{
    class Class1
    {
        public void display()
        {
            Console.WriteLine("Super class display method");
        }
    }
    class Class2 : Class1
    {
        public new void display()
        {
            Console.WriteLine("Sub class display method");
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            Class2 obj = new Class2();
            obj.display();
            Console.ReadKey();
        }
    }
}
Output:

Method Hiding in C#

Example: Method Hiding in C#

In the below example, we are creating the object of Class2 and storing it in the Class1 reference variable. In this case method overriding, the method is going to be executed from the reference type.

namespace PolymorphismDemo
{
    class Class1
    {
        public void display()
        {
            Console.WriteLine("Super class display method");
        }
    }
    class Class2 : Class1
    {
        public new void display()
        {
            Console.WriteLine("Sub class display method");
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            Class1 obj = new Class2();
            obj.display();
            Console.ReadKey();
        }
    }
}

Output: Super class display method

What is the difference between Method Overriding and Method Hiding in C#?

A parent class method can be redefined under its child class using two different approaches.

  1. Method Overriding.
  2. Method Hiding.

In Method Overriding parent class gives permission for its child class to override the method by declaring it as virtual. Now the child class can override the method using the Override keyword as it got permission from the parent.

In Method hiding also, the parent class methods redefined under the child classes even if they were not declared as Virtual in the parent class, and in this case, we need to re-implement the method in the child class by using the ‘new‘ keyword.

In method overriding a base class reference variable pointing to a child class object will invoke the overridden method in the child class. In method hiding a base class reference variable pointing to a child class object will invoke the hidden method in the base class.

New is reference-type specific, overriding is object-type specific.

In the next article, I am going to discuss Partial Class and Partial Methods in C# with Examples. Here, in this article, I try to explain What exactly Method Hiding is and how to implement Method Hiding in C# with Examples. I hope this article will help you with your need. I would like to have your feedback. Please post your feedback, question, or comments about this Method Hiding in C# with Examples article.

3 thoughts on “Method Hiding in C#”

  1. blank

    If a method is simply hidden then the implementation to call is based on the compile-time type of the argument “this” whereas if a method is overridden then the implementation to be called is based on the run-time type of the argument “this”.

    can you please provide the example for this or elaborate

  2. blank

    Hi

    Can you please five and example for the below lines, I am finding it little difficult to understand.

    “If a method is simply hidden then the implementation to call is based on the compile-time type of the argument “this” whereas if a method is overridden then the implementation to be called is based on the run-time type of the argument “this”.”

    Thanks!

    1. blank

      Basically, when you use “hiding”, you need to create an object of the Derived’s class to call the Derived method (e.g Derived derived = new Derived()). Otherwise, if you create a Base’s object that points to a Derived object (e.g Base base = new Derived()), and then call a method like base.YourMethod(), it will call the Base’s method, not the Derived’s one.

      This doesn’t happen when you use virtual. Since virtual happens at run time, and since the most-derived-overridden-method is “chosen” at runtime, you will call the method of the most derived class, even if the object the method was called with is of type Base.

Leave a Reply

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