Introduction
Like the polymorphism and method overriding concepts in C#, there's also a concept to hide the base class's methods from the derived class. The method of hiding the base class's methods from the derived class is known as Method Hiding also known as Method Shadowing. This concept is known as Method Hiding. In this article, we will learn about the C# language's method hiding concept. Let us dive deeper into the topic.
Method Hiding in C#
The method of hiding the base class's methods from the derived class is known as Method Hiding. This method is also known as Method Shadowing. The implementation of the methods of a base class can be hidden from the derived class in method hiding using the new keyword. Or in other words, the base class method can be redefined in the derived class by using the new keyword.
Example 1
Let us write a program to illustrate the concept of method hiding.
using System;
// Base Class
public class My_class {
public void member()
{
Console.WriteLine("Total number of students: 3");
}
}
// Derived Class
public class My_students : My_class {
/* Reimplement the method of the base class using the new keyword
It hides the method of the base class */
public new void member()
{
Console.WriteLine("Name: Rakesh, Marks: 40 \nName: Som, "+
"Marks: 45 \nName: Ria, Marks: 49 ");
}
}
class ninja {
static public void Main()
{
// Creating the object of the derived class
My_students obj = new My_students();
// Accessing the method of derived class
obj.member();
}
}
Output
Name: Rakesh, Marks: 40
Name: Som, Marks: 45
Name: Ria, Marks: 49
In the above code, My_class is the base class and My_students is a derived class. The method name is the same in both the classes, i.e. member() method. But, the member() method is declared with the new keyword in the derived class. Upon calling this method, it prints the name and the marks of the students. It does not print the number of students which means when we call the member() method with the derived class object, it hides the same name method present in the base class due to the presence of the new keyword.
Example 2
Let us write a program to illustrate the concept of method hiding without using the new keyword.
using System;
// Base Class
public class My_students {
public void member()
{
Console.WriteLine("Total number of students: 3");
}
}
// Derived Class
public class My_students : My_class {
public void member()
{
Console.WriteLine("Name: Rakesh, Marks: 40 \nName: Som, "+
"Marks: 45 \nName: Ria, Marks: 49 ");
}
}
class Ninja {
// Main method
static public void Main()
{
// Creating the object of the derived class
My_students obj = new My_students();
// Access the method of derived class
obj.member();
}
}
Output
Name: Rakesh, Marks: 40
Name: Som, Marks: 45
Name: Ria, Marks: 49
If we do not use the new keyword, the compiler will run the code without giving an error, but will give a warning that if you want to hide a method then you can use the new keyword.
prog.cs(18,14): warning CS0108: `My_class.member()’ hides inherited member `My_students()’. Use the new keyword if hiding was intended
prog.cs(9,14): (Location of the symbol related to previous warning)