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 the base class and the derived class both have a method with the same name. When we create an object of the derived class, it will hide the base class method and call its own method. 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.
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.
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 wants to implement some of the methods of the 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.