Method hiding in C Sharp inheritance

Method hiding in C# inheritance- Method hiding occurs in inheritance relationship when base class and derived class both have a method with same name. When we create the object of derived class it will hide the base class method and will call its own method and this is called method hiding or name hiding in C# inheritance.

We use “new” keyword in derived function name to show that implementation of the function in derived class is intentional and derived class no longer want to use base class method.

NOTE: If we do not use “new” keyword then compiler will raise only warning, but, program will work fine.

In below C# program, Base and Derived class both have show() method implementation. On calling show() on derived object will hide the base class show method and call derived class method.

class Base{
    public void show()
    {
        Console.WriteLine("Base...");
    }
}
class Derived : Base
{
    new public void show()
    {
        Console.WriteLine("Derived...");
    }
}
class Program
{
   static void Main(string[] args)
    {
 
        Derived d = new Derived();
        d.show();        
    }
    
}

Output: Derived…

When to use method hiding in C#?

When to use method hiding in C# program is really an important concept that we need to understand. Does child class, no longer want to use some of the methods implementation of parent class, but, want to implement it for its own? Use C# method hiding also called name hiding feature then. Note that we cannot override the method as it’s not virtual in base class.

Let’s understand it by example

In below program, “ApplicationSoftware” (Child) class wants to inherit all operations i.e. update () and install () methods of “Software” (Parent) class. And, ApplicationSoftware class will have one operation i.e. run() method like below program example.

class Software
{
    public void update()
    {
        Console.WriteLine("Base Updating...");
    }
    public void install()
    {
        Console.WriteLine("Base Installing...");
    }
}
class ApplicationSoftware : Software
{
    public void run()
    {
        Console.WriteLine("Child Running...");
    }
}
 
class Program
{
    static void Main(string[] args)
    {        
        ApplicationSoftware appsoft = new ApplicationSoftware();
        appsoft.update();
        appsoft.install();
        appsoft.run();
    }
 
}

Output:

Base Updating…
Base Installing…
Child Running…

At some later pointer of time, if child class ApplicationSoftware wants to implements it own behavior for install() method of base class, it will use name hiding feature without disturbing the base class Software.

Note that,We should not delete base class method implementation to avoid method hiding in software projects, as there are chances that base class is being used by some other classes in a complex software project.

Below is the updated class from above C# programming source code

class ApplicationSoftware : Software
{
    public void run()
    {
        Console.WriteLine("Child Running...");
    }
    //implement method that will hide the base class method
    new public void install()
    {
        Console.WriteLine("Child Installing...");
    }
}

Here is the output for updated child class.

Output:

Base Updating…
Child Installing…
Child Running…

Conclusion:

Use method hiding in C#

  • If child class want to implement some of methods of base class without disturbing base class.
  • If base class is available in a library that cannot be changed even if we want to do so intentionally. Use name hiding in C# then.

NOTE: If somehow, it is possible to change base class and it will not affect a program a lot, we can make the method virtual and override in derived class.

Related Posts