Acquiring (taking) the properties of one class into another class is called inheritance. Inheritance provides reusability by allowing us to extend an existing class.
The reason behind OOP programming is to promote the reusability of code and to reduce complexity in code, which is possible by using inheritance.
The following are the types of inheritance in C#.
The inheritance concept is based on a base class and derived class. Let us see the definition of a base and derived class.
- Base class: is the class from which features are to be inherited into another class.
- Derived class: it is the class in which the base class features are inherited.
It is the type of inheritance in which there is one base class and one derived class.
This is the type of inheritance in which there are multiple classes derived from one base class. This type of inheritance is used when there is a requirement for one class feature that is needed in multiple classes.
In the preceding program, each class is derived from one class that is derived from another class. Hence, this type of inheritance is called Multilevel Inheritance.
Multiple inheritance using Interfaces
C# does not support multiple inheritances of classes. To overcome this problem, we can use interfaces. We will see more about interfaces in my next article in detail..
The following are some key points about inheritance:
- C# does not support multiple inheritances of classes, the same thing can be done using interfaces.
- Private members are not accessed in a derived class when one class is derived from another.
I hope you now have an overview of inheritance and its types. In the next article, we will learn about each inheritance type in detail. If you have any suggestions regarding this article, then please contact me.