Inheritance in Object-Oriented Programming(OOPS)
OOPs come in picture (in comparison to structured programming languages) due to its code reusability
features. This means that we can add
additional features
to an existing class without modifying it. The new class will have the combined features of both the classes.
In C#, it is possible to inherit fields and methods from one class to another. So, inheritance indicates the code reusability
.
What is Inheritance?
Inheritance is a mechanism in which one class (new created class called derived/parent class) acquires the property of another existing class called base/child class.
In below example(W3S example) we have a base class Vehicle in which there is one method/function honk() and we have another class (derived class) Car which is inheriting Vehicle class (NOTE: To inherit from a class, use the : symbol.)
class Vehicle // base class (parent) { public string brand = "Ford"; // Vehicle field public void honk() // Vehicle method { Console.WriteLine("Tuut, tuut!"); } } class Car : Vehicle // derived class (child) { public string modelName = "Mustang"; // Car field }
Types of Inheritance in C#
The inheritance concept is based on a base class and derived class. C# supports four types of inheritances. They are:
1. Single inheritance
It is the type of inheritance in which there is one base class and one derived class. We can take below example for this
public class A // This is Base class { public void fun_A() { Console.WriteLine("This is A"); } } class B : A // B is derived class { // Some functions and fields of B // Now B having all public members/function of class A }
2. Hierarchical inheritance
In this type of inheritance, there is one base (we can say parent class also) class and the other derived classes inherit the same base class to achieve hierarchical inheritance.
public class A // This is Base/Parent class { public void fun_A() { Console.WriteLine("This is A"); } } class B : A // B is derived class { // Some functions and fields of B // Now B having all public members/function of class A } class C : A // C is derived class { // Some functions and fields of C // Now C having all public members/function of class A } class D : A // C is derived class { // Some functions and fields of D // Now D having all public members/function of class A }
3. Multilevel inheritance
In this type of inheritance, one class can inherit from a derived class. Hence, the derived class becomes the base class for the new class.
public class A // This is Base/Parent class { public void fun_A() { Console.WriteLine("This is A"); } } class B : A // B is derived class and A is base class { // Some functions and fields of B // Now B having all public members/function of class A } class C : B // B is base class for C { // Some functions and fields of C // Now C having all public members/function of class A and B } class D : C // C is derived class for D { // Some functions and fields of D // Now D having all public members/function of class A, B and C }
4. Multiple inheritance
When a derived class having more than one base class
then it is called Multiple inheritance. C# does not supports multiple inheritance directly.
We can achieve this feature with help of using interface.
public interface A { void fun_A(); } public interface B { void fun_B(); } class C : A,B { // Some functions and fields of C // Now C needs to implements all members/function of class A and B }
Summary
To summarise, The inheritacne is the feature of object-oriented programming which indicates code- reuseabilty without modifying the base class. If
you don't want make any class as a base class i.e. to prevent any class to be inherited
then use sealed keyword. We'll discuss this things
in another articles. Hope you picked up a thing or two from this article.
Cheers!