...

/

Hiding vs. Overriding a Method

Hiding vs. Overriding a Method

Learn to create a new method with the same signature.

We'll cover the following...

Hide

Hiding creates a method of the same name and signature as it it’s defined in the base class. To hide a method, we use the new keyword:

public new void Voice()
{
}

Instead of providing a method with a different implementation (overriding), the new keyword creates an entirely different method, but with the same name and signature. A method marked with the new keyword isn’t associated by any means with the base version of the method. They’re completely separate entities.

Press + to interact
Program.cs
Animal.cs
Bird.cs
using System;
namespace MethodHiding
{
public class Bird : Animal
{
// We are using the new keyword
// to hide the Voice() method that exists in the parent class
public new void Voice()
{
Console.WriteLine("Some bird voice...");
}
}
}
...