In c#, Inheritance is one of the primary concepts of object-oriented programming (OOP), and it is used to inherit the properties from one class (base) to another (child) class.
The inheritance will enable us to create a new class by inheriting the properties from other classes to reuse, extend, and modify other class members' behavior based on our requirements.
In c# inheritance, the class whose members are inherited is called a base (parent) class, and the class that inherits the members of the base (parent) class is called a derived (child) class.
Following is the syntax of implementing an inheritance to define a derived class that inherits the base class's properties in the c# programming language.
If you observe the above syntax, we are inheriting the base class's properties into the child class to improve code reusability.
Following is the simple example of implementing inheritance in the c# programming language.
If you observe the above example, we defined a class “X” with the method called “GetDetails” and the class “Y” is inheriting from class “X”. After that, we call a “GetDetails” method by using an instance of derived class “Y”.
In c#, it’s not possible to inherit the base class constructors in the derived class. The accessibility of other base class members also depends on the access modifiers that we used to define those members in a base class.
Following is the example of implementing an inheritance by defining two classes in the c# programming language.
If you observe the above example, we defined a base class called “User” and inherited all the user class properties into a derived class called “Details” and we are accessing all the members of the User class with an instance of the Details class.
If we uncomment the commented code, we will get a compile-time error because the Location property in the User class is defined with a private access modifier. The private members can be accessed only within the class.
When you execute the above c# program, you will get the result below.
If you observe the above result, we are able to access all the properties of the base class into the child class based on our requirements.
Generally, c# supports only single inheritance which means a class can only inherit from one base class. However, in c# the inheritance is transitive, and it allows you to define a hierarchical inheritance for a set of types, and it is called a multi-level inheritance.
For example, if class C is derived from class B, and class B is derived from class A, then class C inherits the members declared in both class B and class A.
If you observe the above code snippet, class C is derived from class B, and class B is derived from class A, then class C inherits the members declared in both class B and class A. This is how we can implement multi-level inheritance in our applications.
Following is the example of implementing multi-level inheritance in the c# programming language.
If you observe the above example, we implemented three classes (A, B, C), and class C is derived from class B, and class B is derived from class A.
By implementing a multi-level inheritance, class C can inherit the members declared in class B and class A.
When you execute the above c# program, you will get the result below.
As discussed, c# supports only single inheritance which means a class can only inherit from one base class. If we try to inherit a class from multiple base classes, then we will get compile-time errors.
For example, if class C is trying to inherit from Class A and B simultaneously, we will get a compile-time error because multiple inheritances is not allowed in c#.
If you observe the above code snippet, class C is trying to inherit properties from both classes A and B simultaneously, which will lead to compile-time errors like “Class C cannot have multiple classes: A and B”.
As discussed, multi-level inheritance is supported in c#, but multiple inheritance is not supported. If you want to implement multiple inheritance in c#, we can achieve this by using interfaces. In the next chapters, we will learn how to use interfaces to achieve multiple inheritance in a detailed manner.