What is method hiding in C#?


Method hiding is also known as shadowing. The method of the parent class is available to the child class without using the override keyword in shadowing. The child class has its own version of the same function.

Use the new keyword to perform shadowing.

Let us see an example.

Example

 Live Demo

using System;
using System.Collections.Generic;

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());
   }
}

Output

This is Child Class!

Updated on: 22-Jun-2020

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements