Industrial manufacturing
Industrial Internet of Things | Industrial materials | Equipment Maintenance and Repair | Industrial programming |
home  MfgRobots >> Industrial manufacturing >  >> Industrial programming >> C Language

Mastering C# Inheritance: Concepts, Types, and Practical Code

C# Inheritance Explained

This guide covers the fundamentals of inheritance in C#, its various forms, and practical code examples.

Inheritance in C# lets you create a new class that derives from an existing one, forming a parent (base) and child (derived) relationship. This powerful feature of Object‑Oriented Programming (OOP) promotes code reuse and logical organization.

The base class supplies fields, properties, and methods that the derived class inherits, allowing the child to extend or customize behavior while keeping shared logic in one place.


How to Declare Inheritance in C#

In C#, the colon (:) indicates inheritance:

class Animal {
  // fields and methods
}

// Dog inherits from Animal
class Dog : Animal {
  // fields and methods of Animal
  // fields and methods of Dog
}

Here, Dog extends Animal, gaining access to its members. The diagram below illustrates the relationship.

Mastering C# Inheritance: Concepts, Types, and Practical Code

Example: Basic Inheritance

using System;

namespace Inheritance {

  // Base class
  class Animal {
    public string name;
    public void Display() {
      Console.WriteLine("I am an animal");
    }
  }

  // Derived class
  class Dog : Animal {
    public void GetName() {
      Console.WriteLine("My name is " + name);
    }
  }

  class Program {
    static void Main(string[] args) {
      Dog labrador = new Dog();
      labrador.name = "Rohu";
      labrador.Display();
      labrador.GetName();
      Console.ReadLine();
    }
  }
}

Output

I am an animal
My name is Rohu

In this example, Dog inherits the name field and Display() method from Animal. The derived class can also add its own members, such as GetName().


The "is‑a" Relationship

Inheritance represents an "is‑a" relationship. Use it when one class logically is a specialized form of another.

These relationships justify deriving Dog from Animal, Apple from Fruit, and Car from Vehicle.


Protected Members in Inheritance

When a field or method is declared protected, it is accessible within its own class and all derived classes, but hidden from external code.

Example: Using Protected Methods

using System;

namespace Inheritance {
  class Animal {
    protected void Eat() {
      Console.WriteLine("I can eat");
    }
  }

  class Dog : Animal {
    static void Main(string[] args) {
      Dog labrador = new Dog();
      labrador.Eat();
      Console.ReadLine();
    }
  }
}

Output

I can eat

Because Eat() is protected, the Dog class can invoke it, but code outside the hierarchy cannot.


Types of Inheritance

Inheritance manifests in several patterns:

1. Single Inheritance

A single derived class inherits from one base class.

Mastering C# Inheritance: Concepts, Types, and Practical Code

2. Multilevel Inheritance

A chain of inheritance where a class inherits from a base, then another class inherits from that derived class.

Mastering C# Inheritance: Concepts, Types, and Practical Code

3. Hierarchical Inheritance

Multiple derived classes share the same base class.

Mastering C# Inheritance: Concepts, Types, and Practical Code

4. Multiple Inheritance

While C# does not support inheriting from multiple classes, interfaces provide a flexible alternative for achieving similar behavior.

Mastering C# Inheritance: Concepts, Types, and Practical Code

5. Hybrid Inheritance

A combination of two or more inheritance types, such as multilevel plus hierarchical.

Mastering C# Inheritance: Concepts, Types, and Practical Code

Method Overriding

When a derived class declares a method with the same signature as a base class method marked virtual, the derived method can override it using override. This enables polymorphic behavior.

using System;

namespace Inheritance {
  class Animal {
    public virtual void Eat() {
      Console.WriteLine("I eat food");
    }
  }

  class Dog : Animal {
    public override void Eat() {
      Console.WriteLine("I eat dog food");
    }
  }

  class Program {
    static void Main(string[] args) {
      Dog labrador = new Dog();
      labrador.Eat();
    }
  }
}

Output

I eat dog food

The Eat() method in Dog overrides the one in Animal. The virtual keyword permits the override, while override signals the substitution.


Using the base Keyword

Sometimes you need to invoke the base class implementation from an overriding method. The base keyword facilitates this.

Example: Calling Base Methods

using System;

namespace Inheritance {
  class Animal {
    public virtual void Eat() {
      Console.WriteLine("Animals eat food.");
    }
  }

  class Dog : Animal {
    public override void Eat() {
      base.Eat();
      Console.WriteLine("Dogs eat dog food.");
    }
  }

  class Program {
    static void Main(string[] args) {
      Dog labrador = new Dog();
      labrador.Eat();
    }
  }
}

Output

Animals eat food.
Dogs eat dog food.

Here, base.Eat() calls the Animal implementation before executing the derived class logic.


Why Inheritance Matters in C#

Inheritance enables reusable logic across related classes. Consider geometric shapes where perimeter calculations share a common formula.

  1. Create a RegularPolygon base class with a CalculatePerimeter() method.
  2. Derive Square and Rectangle from it, adding specific properties like Length and Sides.
  3. Each derived class can also implement its own area calculation while reusing the perimeter logic.

Illustrative Example

using System;

namespace Inheritance {
  class RegularPolygon {
    public void CalculatePerimeter(int length, int sides) {
      int result = length * sides;
      Console.WriteLine("Perimeter: " + result);
    }
  }

  class Square : RegularPolygon {
    public int Length = 200;
    public int Sides = 4;
    public void CalculateArea() {
      int area = Length * Length;
      Console.WriteLine("Area of Square: " + area);
    }
  }

  class Rectangle : RegularPolygon {
    public int Length = 100;
    public int Breadth = 200;
    public int Sides = 4;
    public void CalculateArea() {
      int area = Length * Breadth;
      Console.WriteLine("Area of Rectangle: " + area);
    }
  }

  class Program {
    static void Main(string[] args) {
      Square s1 = new Square();
      s1.CalculateArea();
      s1.CalculatePerimeter(s1.Length, s1.Sides);

      Rectangle t1 = new Rectangle();
      t1.CalculateArea();
      t1.CalculatePerimeter(t1.Length, t1.Sides);
    }
  }
}

Output

Area of Square: 40000
Perimeter: 800
Area of Rectangle: 20000
Perimeter: 400

This pattern illustrates how inheritance reduces duplication and enhances maintainability.


C Language

  1. C# Static Keyword: Mastering Static Variables, Methods, and Classes
  2. Master C++ Inheritance: Build Powerful Classes with Reusable Code
  3. C++ Inheritance Models: Multiple, Multilevel, Hierarchical
  4. Mastering Python Inheritance: Concepts, Syntax, and Practical Examples
  5. Master Python Multiple Inheritance, Multilevel Inheritance, and Method Resolution Order (MRO)
  6. Mastering Java Inheritance: Concepts, Types, and Practical Examples
  7. Master C# Inheritance & Polymorphism: Practical Code Examples
  8. Mastering Java Inheritance: Subclassing and Superclass Principles
  9. Mastering C# Inheritance: Build Reusable, Maintainable Code
  10. C# Polymorphism: Static vs Dynamic Binding Explained