Function Hiding in C#

What is Function Hiding?

When we Use the new keyword to hide a base class member, then it is called as Function Hiding. 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. Reimplementing parent class methods under child classes can be done using two different approaches, such as

  1. Function overriding
  2. Function 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 the permission to the child classes for overriding the methods.

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.

Syntax:

Class1:

Public void display()

Class2 : Class1

Public new void display()

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

See the following example for better understanding.
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:

Function Hiding in C#

What is the difference between Method Overriding and Method Hiding?

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 the 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.

Method hiding also parent class methods can be redefined under child classes even if they were not declared as Virtual by using ‘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.

For hiding the base class method from derived class simply declare the derived class method with the new keyword whereas for overriding the base class method in a derived class, we need to declare the base class method as virtual and the derived class method as the override.

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”.

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

SUMMARY:

In this article, I try to explain Function Hiding in C#. 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 article.

Leave a Reply

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