C# Inheritance with Examples

Learn via video courses
Topics Covered

Overview

In today's world of programming, reusability, organization, and extensibility are some of the highly valued attributes. The concept of Object-oriented programming (OOP) provides an effective way to achieve these goals, and one of its core principles, inheritance, plays an important role in achieving them. In C#, a flexible and widely-used programming language, inheritance allows programmers to build a lot of complex systems with much ease and elegance.

What is Inheritance?

Inheritance is a fundamental principle in object-oriented programming that revolves around the concept of code reusability. Inheritance allows a class to inherit the attributes (data members) and behavior (methods) of another class, known as the base class or superclass. The class that inherits from the base class is called the derived class or subclass.

The concept of inheritance can be understood in a better way using real-world relationships. Consider a basic example of a Vehicle. You might have a base class called Vehicle, and from there, you can derive more specific classes like Car, Truck, and Bike. The Vehicle class would contain general characteristics that apply to all the Vehicles such as an engine, brakes, wheels, etc. while the derived classes would have additional features unique to each vehicle type.

How to Perform Inheritance in C#?

In C#, inheritance is established using the colon(:) symbol. The colon(:) symbol is used just after the derived class name which in turn is followed by the base class name.

Let us understand inheritance in C# with the help of a code example.

In the above example, there are three classes - Vehicle, Car, and Truck. The Vehicle class is a base class whereas the other two are the derived classes that inherit the properties of the base class Vehicle.

The inheritance relationship is implemented using the colon(:) symbol followed by the base class name as shown in the code example.

Example

Here is an example of an inheritance relationship established between two entities.

Output:

In the above example, we have inherited a class Car from the Vehicle class. You can see that using an object c of the Car class, we are accessing the members of the Vehicle class such as the name field and the engine() method. All this is possible because of the inheritance relationship that exists between the parent class "Vehicle" and the child class "Car".

C# Inheritance Use Cases

  • C# Inheritance promotes code reusability as it allows the derived classes to reuse the properties and methods of the base class.

  • Inheritance makes code maintenance easier by allowing us to modify the code in the base class and automatically reflect the changes in the derived class.

  • Inheritance is used to show real-world relationships between objects.

    For example: An Animal base class can be inherited by various derived classes such as a Dog and a Cat.

  • Inheritance makes code organization easy by hierarchically organizing the related classes together.

C# Inheritance Protected Members

Protected is an access specifier that is used with classes, methods, or variables to restrict their usage on various parts of the program. When we use a protected keyword with a variable or a method, it can only be accessed within the class or its derived class. An example of the same is as follows:

Output:

As you can see in the above example, we have made the run() method of the base class Vehicle as protected, therefore, this method can only be accessed from the base class Vehicle and the derived class Truck.

Types of Inheritance in C#

Various types of inheritance are supported in C# which are as follows:

Single Inheritance

In single inheritance, a derived class inherits the properties of only one base class.

single inheritance in csharp

As you can see in the above image, class A is the base class which is being inherited by the derived class B.

Let us understand it with the help of a code example.

Output:

In the above example, the Car class is only inherited from the Vehicle class. Therefore, it is an example of single inheritance as only one derived class is inherited from one base class.

Multi-Level Inheritance

In this type of inheritance, a derived class inherits from a base class, and at the same time, the derived class itself also acts as a base class for some other class.

multi-level inheritance in csharp

In the above image, class B inherits the properties of the base class A. However, at the same time, class B also acts as a base class for class C.

Let us understand it with the help of a code example.

Output:

As a real-life example, a Father inherits the properties/attributes of the GrandFather and a Son inherits it from the Father. Therefore, the same relationship has been shown in the above code example with the help of classes for showing multi-level inheritance.

Hierarchical Inheritance

In this type of inheritance, multiple derived classes inherit from the same base class.

hierarchical inheritance in csharp

As shown in the above image, there is a base class that is being inherited by three derived classes.

Let us understand it with the help of a code example.

Output:

In the above example, both the classes Physics and Chemistry inherit from the same base class Science. Therefore, it counts as an example of hierarchical inheritance in C#.

Multiple Inheritance

In this type of inheritance, a single derived class can inherit properties from multiple base classes. One important thing to remember is that C# does not support multiple inheritances with classes. However, C# does support multiple inheritances with interfaces.

multiple inheritance in csharp

As shown in the above image, a single derived class C inherits and implements the methods of interfaces A and B.

Let us understand the concept of Multiple inheritance in C# with the help of a code example.

Output:

In the above example, Car and Bike both are interfaces, and Transport is a derived class that inherits the properties of both the Car and Bike interfaces. Therefore, this example will support Multiple inheritance in C# using interfaces.

Hybrid Inheritance

Hybrid Inheritance is a mixture of two or more kinds of inheritances together. However, since C# does not support multiple inheritance with classes, therefore, hybrid inheritance is also not possible through classes. But it can be implemented using interfaces.

hybrid inheritance in csharp

As you can see in the above image, we have two types of inheritances being implemented. One is the hierarchical inheritance among the classes A, B, and C whereas the second one is multiple inheritances among the classes B, C, and D.

Method Overriding in C# Inheritance

Whenever we have the same method in both the base and the derived classes, then the method in the base class is overridden by the method present in the derived class. This is referred to as Method Overriding in C#.

Let us understand it with the help of a code example.

Output:

As you can see in the above example, the Vehicle and the Bike class has the same method run(). But when we have called the method using the Bike object b, the run() of the Bike class gets executed due to method overriding. The Bike class method has overridden the Vehicle class method.

Moreover, we have also used the virtual and override keywords along with the run() method of the base and derived class respectively. The virtual keyword signifies that the method allows itself to be overridden by the derived class whereas the override keyword signifies that this method is overriding from the base class.

What is the Base Keyword in C# Inheritance?

As described in the previous section, method overriding overrides the method of the base class and displays the only content of the derived class.

However, what if we want to display the base class method as well?

In that scenario, we use the base keyword in C#. The base keyword allows us to call the base class method from the derived class in C#.

Let us understand it with the help of a code example.

Output:

Therefore, as you can see in the above example, we have only called the run() method from the Bike object b, but it still runs the base class method. This is because we have used the base keyword in the derived class Bike, thereby executing the run() method from the base class Vehicle as well.

Advantages of Inheritance in C#

The advantages of inheritance in C# are as follows:

  • Modularity and Code Organization:
    Class hierarchies produced by inheritance enable the organization and modularity of code. Moreover, it also allows the programmers to group the logically related classes under a common base class which makes the code even more organized and manageable.

  • Code Reusability:
    Inheritance also promotes code reuse by allowing the derived classes to access and use the properties of the base classes. It reduces code duplication which also leads to more efficient and maintainable codebases.

  • Code Maintainance:
    With inheritance, since the base class propagates its properties to all the derived classes, it reduces the need to modify all the individual derived classes. This makes the code maintenance even easier as we only need to modify the base class functions keeping all other classes as it is.

Limitations of Inheritance in C#

The limitations of inheritance in C# are as follows:

  • Tight Coupling:
    Inheritance creates a tight coupling between the base and the derived classes. Due to this tight coupling, changes made to the base class severely impact the behavior of the derived classes, making it sensitive to changes.
  • Fragile base class Problem:
    Changing the functionalities of the base class can impact the behavior of the derived classes, thereby making the base class fragile to changes. Also affects the maintainability of the codebases.
  • Increased Complexity:
    Inheritance can increase the complexity of the code by implementing additional levels of abstraction in the code as well as by using excessive code reuse leading to complex class hierarchies.
  • Lack of Multiple inheritance:
    C# does not support multiple inheritance for classes. While multiple interface inheritance is allowed, it cannot directly inherit the implementations from multiple base classes. This can make certain design patterns harder to implement in C#.

FAQs

Q. What is inheritance in C#?

A. Inheritance is a fundamental concept in C# and object-oriented programming that allows a class to inherit the attributes and behaviors of another class. It enables code reuse and promotes modularity and code organization.

Q. Can C# support multiple inheritance?

A. No, C# does not support multiple inheritance for classes. However, C# does support multiple inheritance with interfaces, allowing a class to implement multiple interfaces.

Q. How is inheritance implemented in C#?

A. In C#, inheritance is implemented using the colon (:) symbol followed by the name of the base class after the derived class's declaration. This is used to establish an inheritance relationship between the two classes.

Conclusion

  • Inheritance allows the derived classes to inherit all the properties and behavior of the base classes which promotes code reuse.
  • In C#, inheritance is achieved using the colon(:) symbol.
  • Multiple and hybrid inheritances are supported only through interfaces in C#.
  • Protected methods are accessible only within the same class and the derived class.
  • Whenever we have a method with the same name in both the base and derived classes, the method in the base classes is overridden by the method in the derived class. This is method overriding in C#.
  • The base keyword allows the base class method to be executed by calling it from the derived class in method overriding.