Introduction to Inheritance in C#

C# Tutorials

The object oriented programming (OOP) paradigm has been in use for the past few decades. The key principles of object oriented programming are abstraction, encapsulation, inheritance, and polymorphism.

Inheritance enables developers to create a child class that inherits, extends, or alters the behaviour of its parent. A base class is one from which you can inherit one or more derived classes or child classes. Since inheritance is transitive, programmers may build an inheritance hierarchy for a collection of types.

The concept behind inheritance is to reuse code so we do not have to rewrite it all the time. The idea is to create objects that share traits with their parents. As a result, developers can write code that is efficient, maintainable, and manageable. Using inheritance, developers can inherit methods, properties, and events from existing classes. This programming tutorial examines inheritance, its features and benefits, types, and how it can be implemented in C#.

You can learn more about the aforementioned OOP principles in the tutorials below:

What is Inheritance in C#?

Inheritance is a mechanism that lets you define a parent class, also called the base class, which can then be extended by other classes. Once the base class has been defined, you can use it to create subclasses or child classes.

Programmers can take advantage of inheritance to create new subclasses from the existing base classes. This allows developers to reuse code – and enhance code reusability – while reducing or eliminating code duplication. Inheritance can be single or multiple; it can also be hierarchical or not (like our class tree example).

Inheritance is a feature of OOP that enables developers to inherit the properties and methods of a base class in another class. This means that if you have an existing class called “Vehicle” with some common characteristics like Move and Accelerate, then you can create another class called “Car” which inherits everything from the Vehicle class but adds its own special characteristics like Speed, Model, and Make.

What are the Benefits of Inheritance in C#?

Inheritance has many benefits for C# developers. Inheritance enables you to create a hierarchy of classes, where each class inherits from a base class. The benefits of inheritance include:

  • Code Reuse: Inheritance lets programmers reuse code from base classes in derived classes. This can save you a lot of time and effort when coding, as you do not have to write the same code over again for each new class.
  • Flexibility: Inheritance provides great flexibility in terms of how you structure your code. You can easily change the behaviour of derived classes by overriding methods or adding new ones, without affecting the base class.
  • Extensibility: Inheritance makes it easy to extend the functionality of existing classes by creating new derived classes.

Read: Top Task Management Software for Developers

What are the Different Types of Inheritance

Inheritance has the following types software development:

  • Single: This is the most simple and common type of inheritance in which a single subclass extends a base class.
  • Multiple: In this type of inheritance, you have multiple base classes for a subclass, i.e., there are multiple base classes from which a subclass is derived.
  • Multilevel: This is a type of inheritance in which you have classes extending one another to form a chain.
  • Hierarchical: This is a type of inheritance that looks similar to a family tree.
  • Hybrid: This type of inheritance represents a closed structure.

Note that the C# programming language supports Single, Multilevel, and Hierarchical inheritances only.

How to Program Inheritance in C#

The following C# code example shows two classes – a class named Base and another named Derived. This is an example of Single inheritance in C#:

public class Base
{
    public virtual void Display()
    {
        Console.WriteLine("Inside Base class.");
    }
}
public class Derived: Base
{
    public override void Display()
    {
        Console.WriteLine("Inside Derived class.");
    }
}

Note the usage of the virtual and override keywords in the Display methods of the Base and Derived classes. The virtual keyword is used to specify that this method can be overridden in the child classes, (i.e., it can have alternative implementations in the classes extending the Base class).

Below is an example of method overriding, a concept that allows a method to be overridden in the subclasses. You can also have different methods in the Base and Derived classes, as shown in the example code snippet below:

 
public class Base
{
    public void DisplayBase()
    {
        Console.WriteLine("Inside Base class.");
    }
}
public class Derived: Base
{
    public void DisplayDerived()
    {
        Console.WriteLine("Inside Derived class.");
    }
}

In this case, the Derived class will have access to the method of the Base class by virtue of inheritance.

Multilevel Inheritance in C#

In multilevel inheritance, programmers have more than two classes forming a chain. The following is an example of Multilevel inheritance in C#:

public class A
{
   //Members of class A
}
public class B : A
{
   //Members of the class B
}
public class C : B
{
   //Members of the class C
}

In the preceding code example, note how the classes A, B, and C form a chain of inherited classes.

Read: C# Tools for Code Quality

Hierarchical Inheritance in C#

In hierarchical inheritance, you have more than one subclass extending a common base class as shown in the code snippet below:

public class A
{
  //Members of class A
}
public class B : A
{
    //Members of class B
}
public class C : A
{
    //Members of class C
}

Final Thoughts on Inheritance in C#

Inheritance provides many benefits for developers, such as code reuse, flexibility, extensibility, and polymorphism. However, it is important to consider when to use inheritance in your own projects as it can introduce complexity into your codebase if not used correctly.

With a better understanding of how inheritance works in C# and when to use it appropriately, you will be able to write more efficient and maintainable code.

Read more C# programming tutorials and guides to software development.

Joydip Kanjilal
Joydip Kanjilal
A Microsoft Most Valuable Professional in ASP.NET, Speaker, and Author of several books and articles. More than 25 years of experience in IT with more than 18 years in Microsoft .NET and its related technologies. He was selected as a Community Credit Winner at http://www.community-credit.com several times. He has authored 8 books and more than 500 articles in some of the most reputed sites worldwide including MSDN, Info World, CodeMag, Tech Beacon, Tech Target, Developer, CodeGuru, and more.

More by Author

Get the Free Newsletter!

Subscribe to Developer Insider for top news, trends & analysis

Must Read