What is method hiding in C#?

Method hiding (also known as shadowing) in C# occurs when a derived class defines a method with the same name as a method in its base class, but without using the override keyword. The child class creates its own version of the method that hides the parent class method rather than overriding it.

Method hiding uses the new keyword to explicitly indicate that the method is intended to hide the base class method. This is different from method overriding, which uses override and creates polymorphic behavior.

Syntax

Following is the syntax for method hiding using the new keyword −

public class BaseClass {
   public void MethodName() {
      // base implementation
   }
}

public class DerivedClass : BaseClass {
   public new void MethodName() {
      // hiding implementation
   }
}

Method Hiding vs Method Overriding

Method Hiding vs Method Overriding Method Hiding (new) ? Uses 'new' keyword ? Non-polymorphic ? Base type calls base method ? Compile-time binding ? Hides parent method Method Overriding (override) ? Uses 'override' keyword ? Polymorphic behavior ? Base type calls derived method ? Runtime binding ? Replaces parent method

Using Method Hiding with 'new' Keyword

Example

using System;

class Demo {
   public class Parent {
      public string GetInfo() {
         return "This is Parent Class!";
      }
   }

   public class Child : Parent {
      public new string GetInfo() {
         return "This is Child Class!";
      }
   }

   static void Main(String[] args) {
      Child child = new Child();
      Console.WriteLine(child.GetInfo());
      
      Parent parent = new Child();
      Console.WriteLine(parent.GetInfo());
   }
}

The output of the above code is −

This is Child Class!
This is Parent Class!

Method Hiding vs Overriding Behavior

Example

using System;

class Animal {
   public virtual void Sound() {
      Console.WriteLine("Animal makes a sound");
   }
   
   public void Move() {
      Console.WriteLine("Animal moves");
   }
}

class Dog : Animal {
   public override void Sound() {
      Console.WriteLine("Dog barks");
   }
   
   public new void Move() {
      Console.WriteLine("Dog runs");
   }
}

class Program {
   static void Main() {
      Dog dog = new Dog();
      Animal animal = dog;
      
      Console.WriteLine("=== Direct calls ===");
      dog.Sound();
      dog.Move();
      
      Console.WriteLine("=== Base reference calls ===");
      animal.Sound();
      animal.Move();
   }
}

The output of the above code is −

=== Direct calls ===
Dog barks
Dog runs
=== Base reference calls ===
Dog barks
Animal moves

Comparison Table

Aspect Method Hiding (new) Method Overriding (override)
Keyword new override
Polymorphism No Yes
Base class requirement Any method Must be virtual, abstract, or override
Base reference behavior Calls base class method Calls derived class method

When to Use Method Hiding

Method hiding is useful when you want to provide a different implementation in a derived class but don't want polymorphic behavior. It's commonly used when extending third-party classes where you cannot modify the base class to make methods virtual.

Conclusion

Method hiding in C# uses the new keyword to hide a base class method without polymorphic behavior. Unlike method overriding, the hidden method is called based on the compile-time type of the reference, making it useful for non-polymorphic scenarios where you need different behavior in derived classes.

Updated on: 2026-03-17T07:04:35+05:30

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements