Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Last Updated: Mar 27, 2024
Difficulty: Easy

C# Method Hiding

Leveraging ChatGPT - GenAI as a Microsoft Data Expert
Speaker
Prerita Agarwal
Data Specialist @
23 Jul, 2024 @ 01:30 PM

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)
Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

How to call a hidden method?

There are three ways to call a hidden method:

1. By using the base keyword, a hidden method of the base class can be called in the derived class as in the following example

using System;
public class X {
  public void Z(){
  Console.WriteLine("Base Class");
 }
}

public class Y : X {
  public new void Z(){
  base.Z();
  Console.WriteLine("Derived Class");
 }
}

class Ninja {
  static public void Main(){
  Y ob = new Y();
  ob.Z();
 }
}

 

2. By casting the derived class type to base class type you can invoke the hidden method. As shown in the below example. 

using System;
public class X {
  public void Z(){
  Console.WriteLine("Base Class");
 }
}

public class Y : X {
  public new void Z() {
  Console.WriteLine("Derived Class");
 }
}

class Ninja {
  static public void Main(){
  Y obj = new Y();
  // By type casting
  ((X)obj).Z();
 }
}

 

3. Using the parent class reference variable for calling the hidden method,  instead of using derived class reference variable as in the following example:

using System;
  public class X {
  public void Z(){
  Console.WriteLine("Base Class");
 }
}

public class Y : X {
  public new void Z() {
  Console.WriteLine("Derived Class");
 }
}

class Ninja {
  static public void Main(){
  // Invoking the hidden method
  X ob = new Y();
  ob.Z();
 }
}

Frequently Asked Questions

What is 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.

What is the difference between method overriding and method hiding in C#?

In method overriding, when a base class reference variable pointing to the object of the derived class, then it will call the overridden method in the derived class. In the method hiding, when the base class reference variable pointing to the object of the derived class, then it will call the hidden method in the base class.

Also Read About - singleton design pattern in c#   

Conclusion

In this article, we have extensively discussed  the C# language's method hiding concept Having gone through this article, I am sure you must be excited to read similar blogs. Coding Ninjas has got you covered. Here are some similar blogs to redirect: C# Method OverridingC# Method ParametersMethods in C#Introduction to C#C# Type Conversion and Properties of C# We hope that this blog has helped you enhance your knowledge, and if you wish to learn more, check out our Coding Ninjas Blog site and visit our Library. Here are some courses provided by Coding Ninjas: Basics of C++ with DSACompetitive Programming and MERN Stack Web Development. Do upvote our blog to help other ninjas grow.

Happy Learning! 

Topics covered
1.
Introduction
2.
Method Hiding in C#
2.1.
Example 1
2.2.
Output
2.3.
Example 2
2.4.
Output
3.
How to call a hidden method?
4.
Frequently Asked Questions
4.1.
What is method hiding in C#?
4.2.
What is the difference between method overriding and method hiding in C#?
5.
Conclusion