C# Inheritance

Inheritance is a core principle of Object-Oriented Programming (OOP) that enables a class to acquire the properties and behaviours (methods) from another class. In C#, inheritance enables code reuse, hierarchical classification, and the ability to extend the functionality.

In simple terms, think of inheritance like a family tree for classes in programming. Just like how children inherit traits from their parents, in C# one class can inherit features from another class.

What is Inheritance?

  • It's like creating a new class that automatically gets all the good stuff from an existing class
  • The original class is called the "parent" or "base" class and the new class that inherits the base class is called the "child" or "derived" class

Types of Inheritance in C#:

  1. Single Inheritance: A class is allowed to inherit from just one base/parent class.

  2. Multilevel Inheritance: A class can inherit from another class that has already inherited from a parent class.

  3. Hierarchical Inheritance: Several classes can inherit from a single parent class.

  4. Interface Inheritance: A class can implement one or more interfaces.


Example 1 : Single Inheritance:


using System;

class Animal
{
    public string Name { get; set; }

    public void Eat()
    {
        Console.WriteLine("Eating... ");
    }

    public void Sleep()
    {
        Console.WriteLine("Sleeping...");
    }
}

class Dog : Animal
{
    public void Bark()
    {
        Console.WriteLine("Woof... ");
    }
}

class Program
{
    static void Main()
    {
        Dog myDog = new Dog();
        myDog.Name = "Buddy";
        Console.WriteLine("Dog's name: " + myDog.Name);
        
        myDog.Eat(); // Inherited method
        myDog.Sleep(); // Inherited method
        myDog.Bark(); // Method specific to Dog
    }
}

The cool thing is that myDog object can do everything an Animal can do (like eat and have a name), plus its own special thing (woof)!

Output:


Dog's name: Buddy
Eating...
Sleeping...
Woof...

Explanation:

  • Dog is a child class that inherits from the parent class Animal.

  • The Dog class can access the Eat() and Sleep() methods because they are inherited from Animal.

  • The Bark() method is specific to Dog.


Example 2: Multilevel Inheritance


using System;

class Animal
{
    public void Eat()
    {
        Console.WriteLine("Eating... ");
    }
}

class Mammal : Animal
{
    public void Walk()
    {
        Console.WriteLine("Walking... ");
    }
}

class Dog : Mammal
{
    public void Bark()
    {
        Console.WriteLine("Woof...");
    }
}

class Program
{
    static void Main()
    {
        Dog dog = new Dog();
        dog.Eat();  // Inherited from Animal
        dog.Walk(); // Inherited from Mammal
        dog.Bark(); // Specific to Dog
    }
}

Output:


Eating... 
Wallking...
Woof...

Explanation:

  • Dog class inherits from Mammal class, and Mammal inherits from Animal. So, Dog indirectly inherits methods from both Mammal and Animal classes.


Example 3: Hierarchical Inheritance


using System;

class Animal
{
    public void Eat()
    {
        Console.WriteLine("Eating... ");
    }
}
class Dog : Animal
{
    public void Bark()
    {
        Console.WriteLine("Woof Woof...");
    }
}

class Cat : Animal
{
     public void Meow()
    {
        Console.WriteLine("Meow Meow...");
    }
}

class Program
{
     static void Main()
    {
        Dog dog = new Dog();
        dog.Eat();  // Inherited from Animal
        dog.Bark(); // Specific to Dog

        Cat cat = new Cat();
        cat.Eat();  // Inherited from Animal
        cat.Meow(); // Specific to Cat
    }
}

Output:


Eating... 
Woof Woof...
Eating...
Meow Meow...

Explanation:

  • Here, both Dog class and Cat class inherit from the same parent class Animal class.

  • Both the classes can use the Eat() method from the Animal class.


Example 4: Using base Keyword

The base keyword is used to call a constructor or method of the base class from the derived class.


using System;

class Animal
{
    public Animal(string name)
    {
        Console.WriteLine("Animal constructor called. Name: " + name);
    }

    public void Eat()
    {
        Console.WriteLine("Eating...");
    }
}

class Dog : Animal
{
    public Dog(string name) : base(name)  // Here calling the base class Constructor
    {
        Console.WriteLine("Dog constructor called.");
    }

    public void Bark()
    {
        Console.WriteLine("Barking...");
    }
}

class Program
{
    static void Main()
    {
        Dog dog = new Dog("Buddy");
        dog.Eat();       // Inherited from Animal
        dog.Bark();      // Specific to Dog
    }
}

Output:


Animal constructor called. Name: Buddy
Dog constructor called.
Eating...
Barking...

Explanation:

  • In above code , the Dog class uses the base(name) to call the constructor of the Animal class, which requires a name parameter.


Example 5: Interface Inheritance

Unlike classes, interfaces cannot have any implementation, only the signature of methods, properties, or events. A class or struct that implements an interface is required to provide an implementation for all members of that interface.


using System;

interface IAnimal
{
    void Eat();
    void Sleep();
}

interface IDomesticAnimal
{
    void Play();
}

 class Dog : IAnimal, IDomesticAnimal
{
    public void Eat()
    {
        Console.WriteLine("The dog is eating.");
    }

    public void Sleep()
    {
         Console.WriteLine("The dog is sleeping.");
    }

    public void Play()
    {
        Console.WriteLine("The dog is playing.");
    }
}

class Program
{
    static void Main()
    {
        Dog dog = new Dog();
        dog.Eat();         // Implemented from IAnimal
        dog.Sleep();       // Implemented from IAnimal
        dog.Play();        //   Implemented from IDomesticAnimal
    }
}

Explanation:

  • The Dog class implements the IAnimal interface, meaning it inherits the Eat() and Sleep() methods from IAnimal.
  • The Dog class implements the IDomesticAnimal interface, so it must implement Play() method as well.

Output:


Dog is eating. 
The dog is sleeping.
The dog is playing.
Conclusion:
  • Inheritance helps us in creating a hierarchy of classes where base classes provides common functionality that can be reused or extended by the derived classes.
  • Inheritance :
    1. promotes code reuse,
    2. reduces redundancy,
    3. and helps organize code better.