Background
In this article we will learn about one of the most reusable object oriented features of C#, inheritance. We will learn about inheritance from the basics because I have written this article focusing on students and beginners. Before proceeding further please refer to my previous articles for a better understanding.
What Inheritance is
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 and it 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.
Single inheritance
It is the type of inheritance in which there is one base class and one derived class.
For example,
- public class Accountcreditinfo
- {
- public string Credit()
- {
- return "balance is credited";
- }
- }
- public class debitinfo : Accountcreditinfo
- {
- public string debit()
- {
- Credit();
- return "balance is debited";
- }
- }
In the preceding sample program Accountcreditinfo is the base class and debitinfo is the derived class.
Hierarchical inheritance
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 of one class feature that is needed in multiple classes.
For example,
- class A
- {
- public string msg()
- {
- return "this is A class Method";
- }
- }
- class B : A
- {
- public string info()
- {
- msg();
- return "this is B class Method";
- }
- class C : A
- {
- public string getinfo()
- {
- msg();
- return "this is B class Method";
- }
- }
- }
In the preceding program one base class is derived in many classes hence it is a called a Hierarchical Inheritance.
Multilevel inheritance
When one class is derived from another derived class then this type of inheritance is called multilevel inheritance.
For example,
- public class Person
- {
- public string persondet()
- {
- return "this is the person class";
- }
- }
- public class Bird : Person
- {
- public string birddet()
- {
- persondet();
- return "this is the birddet Class";
- }
- }
- public class Animal : Bird
- {
- public string animaldet()
- {
- persondet();
- birddet();
- return "this is the Animal Class";
- }
- }
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.
For example,
- public interface IA
- {
- string setImgs(string a);
- }
- public interface IB
- {
- int getAmount(int Amt);
- }
- public class ICar : IA, IB
- {
- public int getAmount(int Amt)
- {
- return 100;
- }
- public string setImgs(string a)
- {
- return "this is the car";
- }
- }
In the preceding program the ICar class inherits the features of the two interfaces hence this type of inheritance is called Multiple Inheritance.
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.
Summary
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.