What is method hiding in C# inheritance? Uses with example

(Last Updated On: February 26, 2019)

Method hiding in C# inheritance and when to use method hiding interview question is frequently asked interview question we should focus on. Method hiding in C# program is also called method name hiding. We will answer what is method hiding in C# language with example program.

Answer: 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.

 

 

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.

 

Recommended to read C# method overriding with example.

 

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.

 

 

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

 

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.