Back to course

Polymorphism and Method Overriding

.NET Zero to Hero: Master C# and Modern App Development

Polymorphism

Polymorphism means "many shapes". In C#, it allows a derived class to provide a specific implementation of a method that is already defined in its base class.

Keywords:

  • virtual: Used in the base class to allow overriding.
  • override: Used in the child class to redefine the method.

csharp class Animal { public virtual void MakeSound() => Console.WriteLine("Animal sound"); }

class Dog : Animal { public override void MakeSound() => Console.WriteLine("Bark!"); }