C# Inheritance - Complete Guide
Introduction to Inheritance
Inheritance in C# is a fundamental object-oriented programming concept that allows a class (derived class) to inherit members from another class (base class). This enables code reuse, polymorphism, and the creation of hierarchical relationships between classes.
C# supports single inheritance for classes (a class can inherit from only one base class) but allows multiple interface inheritance. Inheritance promotes the 'is-a' relationship between objects.
Key Features of Inheritance
• Promotes code reuse: common logic goes into the base class, specialized behavior into derived classes.
• Supports polymorphism: base references can point to derived objects, allowing different behavior at runtime.
• Enables extensibility: new functionality can be added to derived classes without changing the base class.
• Maintains hierarchy: represents natural relationships (Dog is an Animal, Car is a Vehicle).
Types of Inheritance in C#
1. Single Inheritance – A class inherits from a single base class.
2. Multilevel Inheritance – A class derives from another derived class.
3. Hierarchical Inheritance – Multiple classes derive from the same base class.
4. Multiple Inheritance – Not supported directly for classes, but possible with multiple interfaces.
Important Keywords in Inheritance
`base` – Used to call base class constructors or access base class members.
`virtual` – Declares a method in the base class that can be overridden.
`override` – Used in derived classes to override virtual/abstract methods.
`sealed` – Prevents further inheritance or method overriding.
`abstract` – Defines incomplete classes/methods that must be implemented in derived classes.
Implementing Inheritance
Here are examples of inheritance in C#:
using System;
namespace InheritanceExample
{
// Base class
public class Animal
{
public string Name { get; set; }
public int Age { get; set; }
public Animal(string name, int age)
{
Name = name;
Age = age;
}
public virtual void MakeSound()
{
Console.WriteLine("The animal makes a sound");
}
public void Eat() => Console.WriteLine($"{Name} is eating");
public void Sleep() => Console.WriteLine($"{Name} is sleeping");
}
// Derived class 1
public class Dog : Animal
{
public string Breed { get; set; }
public Dog(string name, int age, string breed) : base(name, age)
{
Breed = breed;
}
public override void MakeSound() => Console.WriteLine($"{Name} says: Woof! Woof!");
public void Fetch() => Console.WriteLine($"{Name} is fetching the ball");
}
// Derived class 2
public class Cat : Animal
{
public bool IsIndoor { get; set; }
public Cat(string name, int age, bool isIndoor) : base(name, age)
{
IsIndoor = isIndoor;
}
public override void MakeSound() => Console.WriteLine($"{Name} says: Meow! Meow!");
public void Climb() => Console.WriteLine($"{Name} is climbing a tree");
}
// Multi-level inheritance
public class Puppy : Dog
{
public int MonthsOld { get; set; }
public Puppy(string name, int months, string breed) : base(name, months/12, breed)
{
MonthsOld = months;
}
public override void MakeSound() => Console.WriteLine($"{Name} says: Yip! Yip!");
public void Play() => Console.WriteLine($"{Name} is playing with toys");
}
class Program
{
static void Main(string[] args)
{
Animal genericAnimal = new Animal("Generic", 3);
Dog dog = new Dog("Buddy", 5, "Golden Retriever");
Cat cat = new Cat("Whiskers", 2, true);
Puppy puppy = new Puppy("Max", 6, "Labrador");
Console.WriteLine("=== Animal Sounds ===");
genericAnimal.MakeSound();
dog.MakeSound();
cat.MakeSound();
puppy.MakeSound();
Console.WriteLine("\n=== Common Methods ===");
dog.Eat();
cat.Sleep();
Console.WriteLine("\n=== Specific Methods ===");
dog.Fetch();
cat.Climb();
puppy.Play();
Console.WriteLine("\n=== Polymorphism Example ===");
Animal[] animals = new Animal[] { genericAnimal, dog, cat, puppy };
foreach (var animal in animals)
{
animal.MakeSound();
}
Console.WriteLine("\n=== Type Checking ===");
Console.WriteLine($"dog is Animal: {dog is Animal}");
Console.WriteLine($"dog is Dog: {dog is Dog}");
Console.WriteLine($"puppy is Dog: {puppy is Dog}");
Console.WriteLine($"puppy is Animal: {puppy is Animal}");
}
}
}
=== Animal Sounds === The animal makes a sound Buddy says: Woof! Woof! Whiskers says: Meow! Meow! Max says: Yip! Yip! === Common Methods === Buddy is eating Whiskers is sleeping === Specific Methods === Buddy is fetching the ball Whiskers is climbing a tree Max is playing with toys === Polymorphism Example === The animal makes a sound Buddy says: Woof! Woof! Whiskers says: Meow! Meow! Max says: Yip! Yip! === Type Checking === dog is Animal: true dog is Dog: true puppy is Dog: true puppy is Animal: true