Inheritance

What is inheritance? Explain different types of inheritance.

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.

 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.

The following are the types of inheritance in C#.

The inheritance concept is based on a base class 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,

  1. public class Accountcreditinfo //base class    
  2. {  
  3.     public string Credit()  
  4.     {  
  5.         return "balance is credited";  
  6.     }  
  7. }  
  8. public class debitinfo : Accountcreditinfo //derived class    
  9. {  
  10.     public string debit()  
  11.     {  
  12.         Credit();                       ////derived class method    
  13.         return "balance is debited";  
  14.     }  
  15. }  

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,

  1. class A  //base class    
  2. {  
  3.     public string msg()  
  4.     {  
  5.         return "this is A class Method";  
  6.     }  
  7. }  
  8. class B : A  
  9. {  
  10.     public string info()  
  11.     {  
  12.         msg();  
  13.         return "this is B class Method";  
  14.     }  
  15.     class C : A  
  16.     {  
  17.         public string getinfo()  
  18.         {  
  19.             msg();  
  20.             return "this is B class Method";  
  21.         }  
  22.     }  
  23. }    

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,

  1. public class Person  
  2. {  
  3.     public string persondet()  
  4.     {  
  5.         return "this is the person class";  
  6.     }  
  7. }  
  8. public class Bird : Person  
  9. {  
  10.     public string birddet()  
  11.     {  
  12.         persondet();  
  13.         return "this is the birddet Class";  
  14.     }  
  15. }  
  16. public class Animal : Bird  
  17. {  
  18.     public string animaldet()  
  19.     {  
  20.         persondet();  
  21.         birddet();  
  22.         return "this is the Animal Class";  
  23.     }  
  24. }   

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.

For example,

  1. public interface IA //ineterface  1    
  2. {  
  3.     string setImgs(string a);  
  4. }  
  5. public interface IB  //Interface 2    
  6. {  
  7.     int getAmount(int Amt);  
  8. }  
  9. public class ICar : IA, IB //implementatin    
  10. {  
  11.     public int getAmount(int Amt)  
  12.     {  
  13.         return 100;  
  14.     }  
  15.     public string setImgs(string a)  
  16.     {  
  17.         return "this is the car";  
  18.     }  
  19. }    

In the preceding program the ICar class inherits the features of the two interfaces hence this type of inheritance is called Multiple Inheritance.

Leave Comment

Important Topics

Title
Object Oriented Programming (OOP)
OOP vs POP
Class with Example
Objects
Access Modifiers
Encapsulation with example
C# Properties (GET, SET)
Method and Function
Abstraction
Polymorphism
Operator Overloading
Inheritance
Constructor and its Types
Exception Handling
Throwing an Exception