Method Hiding in c#

Method hiding is same as method overriding. By default base class methods are available in derived class. If we want to add new method for derived class with same name and signature, in that case we use method hiding concept. In method hiding, we hide the method of base class in derived class by using new keyword.

See the Example below without new keyword:

using System;

namespace ConsoleApplication4
{
    class Program
    {
        static void Main(string[] args)
        {
            Employee Emp = new Employee();

            Emp.displayEmployee();

            Console.ReadLine();
        }
    }

    public class DisplayEmployee
    {
        public void displayEmployee()
        {
            Console.Write("DisplayEmployee: Display Employee \n");
        }
    }

    class Employee : DisplayEmployee
    {
      
    }
}

Output:
DisplayEmployee: Display Employee

See above code snippet all members of base class is available in derived class. If we want to add new implementation in derived class we add method in derived class also like code below:

using System;

namespace ConsoleApplication4
{
    class Program
    {
        static void Main(string[] args)
        {
            Employee Emp = new Employee();

            Emp.displayEmployee();

            Console.ReadLine();
        }
    }

    public class DisplayEmployee
    {
        public void displayEmployee()
        {
            Console.Write("DisplayEmployee: Display Employee \n");
        }
    }

    class Employee : DisplayEmployee
    {
        public void displayEmployee()
        {
            Console.Write("Employee: Display Employee \n");
        }
    }
}

Output:
Employee: Display Employee

But if you go and run this program there is one warning in warning window like below:
'ConsoleApplication4.Employee.displayEmployee()' hides inherited member 'ConsoleApplication4.DisplayEmployee.displayEmployee()'. Use the new keyword if hiding was intended.

As given in the warning we have to use new keyword in derived class for this method so warning not to be shown. See below code:
using System;

namespace ConsoleApplication4
{
    class Program
    {
        static void Main(string[] args)
        {
            Employee Emp = new Employee();

            Emp.displayEmployee();

            Console.ReadLine();
        }
    }

    public class DisplayEmployee
    {
        public void displayEmployee()
        {
            Console.Write("DisplayEmployee: Display Employee \n");
        }
    }

    class Employee : DisplayEmployee
    {
        public new void displayEmployee()
        {
            Console.Write("Employee: Display Employee \n");
        }
    }
}

Output:
Employee: Display Employee

If we create object of derived class using base class, compiler always call base class method. Please see below code you will get idea:

using System;

namespace ConsoleApplication4
{
    class Program
    {
        static void Main(string[] args)
        {
            int a = 2,
                b = 3;

            baseClass emp;

            emp = new baseClass();

            emp.Add(a, b);

            emp = new DerivedClass();

            emp.Add(a, b);

            Console.ReadLine();
        }
    }

    class baseClass
    {
        public void Add(int a, int b)
        {
            Console.Write("Base Class: Addition\n");
        }
    }

    class DerivedClass : baseClass
    {
        public new void Add(int a, int b)
        {
            Console.Write("Derived Class: Addition\n");
        }
    }
}

Output:
Base Class: Addition
Base Class: Addition



Post a Comment

0 Comments