C# Inheritance


Inheritance is a fundamental concept in object-oriented programming (OOP) that allows a class (known as a derived or child class) to inherit fields and methods from another class (known as a base or parent class). In C#, inheritance promotes code reuse, enhances organization, and establishes a natural hierarchy among classes. Here's a detailed explanation of inheritance in C#, including its features, types, and examples.

Features of Inheritance

  1. Code Reusability: Inheritance allows you to reuse existing code, reducing redundancy and improving maintainability. The derived class inherits the functionality of the base class.

  2. Hierarchical Classification: It provides a way to classify related classes in a hierarchical structure, making it easier to manage and understand complex systems.

  3. Method Overriding: Derived classes can override methods from the base class, allowing for customized behavior while still retaining the base class's implementation.

  4. Access Modifiers: Inheritance respects access modifiers, meaning that only public and protected members of the base class are accessible in the derived class.

  5. Single and Multiple Inheritance: C# supports single inheritance (a class can inherit from only one base class) but does not support multiple inheritance (a class cannot inherit from multiple base classes directly). However, a class can implement multiple interfaces.

Types of Inheritance

  1. Single Inheritance: A class inherits from a single base class.

    public class Animal // Base class { public void Eat() { Console.WriteLine("Eating..."); } } public class Dog : Animal // Derived class { public void Bark() { Console.WriteLine("Barking..."); } }
  2. Multilevel Inheritance: A class inherits from another derived class.

    public class Animal // Base class { public void Eat() { Console.WriteLine("Eating..."); } } public class Dog : Animal // Derived class { public void Bark() { Console.WriteLine("Barking..."); } } public class Puppy : Dog // Further derived class { public void Weep() { Console.WriteLine("Weeping..."); } }
  3. Hierarchical Inheritance: Multiple classes inherit from a single base class.

    public class Animal // Base class { public void Eat() { Console.WriteLine("Eating..."); } } public class Dog : Animal // Derived class 1 { public void Bark() { Console.WriteLine("Barking..."); } } public class Cat : Animal // Derived class 2 { public void Meow() { Console.WriteLine("Meowing..."); } }
  4. Interface Inheritance: A class can implement multiple interfaces.

    public interface IAnimal { void Eat(); } public interface IMammal { void GiveBirth(); } public class Dog : IAnimal, IMammal { public void Eat() { Console.WriteLine("Eating..."); } public void GiveBirth() { Console.WriteLine("Giving birth..."); } }

Method Overriding

In a derived class, you can override a method from the base class using the override keyword. To allow a method to be overridden, you must declare it as virtual in the base class.

Example:

public class Animal { public virtual void Speak() // Base class method { Console.WriteLine("Animal speaks"); } } public class Dog : Animal { public override void Speak() // Overriding base class method { Console.WriteLine("Dog barks"); } } // Usage Animal myDog = new Dog(); myDog.Speak(); // Outputs: Dog barks

Access Modifiers

In C#, you can control the visibility of class members using access modifiers:

  • Public: Accessible from anywhere.
  • Protected: Accessible only within the same class and derived classes.
  • Private: Accessible only within the same class.
  • Internal: Accessible only within the same assembly.

Summary

Inheritance in C# is a powerful mechanism that promotes code reuse and establishes relationships between classes. By allowing derived classes to inherit and override the functionality of base classes, C# facilitates the creation of complex and organized applications. Understanding how to effectively use inheritance will help you design scalable and maintainable object-oriented software.