Inheritance and Polymorphism in C#
From the C# Programming Language cheat sheet · Object-Oriented Programming · verified Jul 2026
Inheritance and Polymorphism
Class inheritance and method overriding
csharp
// Basic inheritance
public class Animal
{
public virtual void MakeSound()
{
Console.WriteLine("Some sound");
}
}
public class Dog : Animal
{
public override void MakeSound()
{
Console.WriteLine("Woof!");
}
}
// Usage
Animal myPet = new Dog();
myPet.MakeSound(); // "Woof!"💡 Abstract classes cannot be instantiated directly
⚡ Virtual methods can be overridden in derived classes
📌 base keyword calls parent class constructor or methods
🟢 Polymorphism allows treating derived objects as base type