In object-oriented programming, Inheritance is one of the most important concepts because it allows the creation of hierarchical classifications. It is the mechanism by which one class is allowed to inherit the features(fields, properties, indexers, and methods) of another class. It provides the ability to reuse the same code, instead of writing the same code again and again.
In C#, a class that is inherited is called a base class. The class that does the inheriting is named a derived class. Therefore, a derived class is a specialized version of a base class. It inherits all of the fields, methods, properties, and indexers defined by the base class (except private members )and adds its own unique elements.
Inheritance supports the concept of reusability. When we want to create a new class and there is already a class that includes some common code that we want, we can derive our new class from the existing class. By doing this, we are reusing the fields, methods, properties, and indexers of the existing class.
In C#, we can inherit a class with the help of the colon operator (:).
C# supports the following four types of inheritance:
- Single Inheritance.
- Multilevel Inheritance.
- Hierarchical Inheritance.
- Multiple Inheritance (only with interfaces).