Inheritance With Different Scenarios In C#

Synopsis

  1. Introduction
  2. Definitions
  3. Advantages of inheritance
  4. Types of inheritance
  5. Reason why it does not support multiple inheritance
  6. Different scenarios with variables
  7. Different scenarios with Methods
  8. Conclusion

Introductions

Inheritance is an important oops concept in programming languages. Without the help of inheritance its difficult to complete any application or project. Inheritance can be used in different scenarios. This article explains how to use inheritance in different ways and in different scenarios.

Definition

Inheritance is acquiring data from one class to another class. Creating a new class from another class is called inheritance.



Advantages of inheritance

Main advantage of inheritance is reusability.

Reusability means that one can use one class propriety or variables and methods can be used from one class to another through inheritance.

In Inheritance, we can use reusability with encapsulation. With the help of access specifiers one can enable encapsulation for variable and method.

Types of inheritance

There are different types of inheritance available.

  • Single Inheritances.
  • Multi Level Inheritance.
  • Multiple Inheritances.
  • Hierarchy Inheritance.
  • Hybrid Inheritance.

Reason for why does not support multiple inheritance

Multiple inheritance is not supported in C# as well as it is not supported in Java. Multiple inheritances does not support because of the diamond problem. Diamond problem means let say ‘A’ and ‘B’ are two classes and they have separate methods of the same name.

Two classes inherited to another class “C”. Create object for “C” class and try to access method at that time. While accessing, the compiler will get confused to access the method because the two class methods are of the same name. This is called diamond problem.



Different Scenario with variables

In inheritance, we can use variables with different scenario. Variable can be accessed with the help of access specifiers.

Type 1

  1. public class First  
  2. {  
  3.     public int varA;  
  4. }  
  5. public class Second : First  
  6. {  
  7.     public int varB;  
  8. }  
  9.   
  10. class Program : First  
  11. {  
  12.     static void Main(string[] args)  
  13.     {  
  14.         Second obj = new Second();  
  15.         obj.varA = 10;  
  16.         obj.varB = 20;  
  17.         Console.WriteLine("A Value:{0}",obj.varA);  
  18.         Console.WriteLine("B Value:{0}", obj.varB);  
  19.         Console.Read();  
  20.     }  
  21. }  
Sample Output

A Value:10

B Value:20


Explanation


In Type 1, we have two classes “First” and “Second”. “First” class inherited into “Second” class. After that create an object to “Second” class and access “First” and “second" class variable through the object created. The two classes in the above program show have a public specifier to access class variable, otherwise you cannot access it in the main function using “obj” object.

Type 2
  1. public class First     
  2. {    
  3.     public int varA;    
  4. }    
  5. public class Second: First    
  6. {    
  7.     public int varB;    
  8. }    
  9.     
  10. class Program: First     
  11. {    
  12.     static void Main(string[] args)     
  13.     {    
  14.         First obj = new Second();    
  15.         obj.varA = 10;    
  16.         obj.varB = 20;    
  17.         Console.WriteLine("A Value:{0}", obj.varA);    
  18.         Console.WriteLine("B Value:{0}", obj.varB);    
  19.         Console.Read();    
  20.     }    
  21. }  
Sample Output

This program does not run and displays an error.

Explanation

In Type 2, we have two classes “First” and “Second”. “First” class inherited into “Second” classes, then create an object to “First” class using “Second” class constructor and access only “First” class variable through the created object. Now, you cannot access “Second” class variable using the created object. If you will try to access it, then it will show error.

Type 3
  1. public class First   
  2. {  
  3.     public int varA;  
  4.     public First()   
  5.     {  
  6.         Console.WriteLine("First");  
  7.     }  
  8.   
  9. }  
  10. public class Second: First   
  11. {  
  12.     public int varB;  
  13.     public Second()   
  14.     {  
  15.         Console.WriteLine("Second");  
  16.     }  
  17. }  
  18.   
  19. class Program: First   
  20. {  
  21.     static void Main(string[] args)   
  22.     {  
  23.         Second obj = new Second();  
  24.         obj.varA = 10;  
  25.         obj.varB = 20;  
  26.         Console.WriteLine("A Value:{0}", obj.varA);  
  27.         Console.WriteLine("B Value:{0}", obj.varB);  
  28.         Console.Read();  
  29.     }  
  30. }  
Sample Output

First
Second
A Value:10
B Value:20


Explanation

In Type 3, we have two classes “First” and “Second”. “First” class inherited into the “Second” classes and then create an object to “Second” class and access “First” “Second” class variable through the created object. Creating object to class automatically calls the constructor while allocating memory to the created object. First call base class constructor and after that call derived class constructor.

Type 4
  1. public class First   
  2. {  
  3.     public int varA;  
  4.     public First()   
  5.     {  
  6.         Console.WriteLine("First");  
  7.     }  
  8.   
  9. }  
  10. public class Second: First   
  11. {  
  12.     public int varB;  
  13.     public Second()   
  14.     {  
  15.         Console.WriteLine("Second");  
  16.     }  
  17. }  
  18.   
  19. class Program: First 
  20. {  
  21.     static void Main(string[] args)   
  22.     {  
  23.         First obj = new Second();  
  24.         obj.varA = 10;  
  25.         //obj.varB = 20;        
  26.         Console.WriteLine("A Value:{0}", obj.varA);  
  27.         //Console.WriteLine("B Value:{0}", obj.varB);  
  28.         Console.Read();  
  29.     }  
  30. }  
Sample Output

First
Second
A Value:10


Explanation

In Type 4, we have two classes “First” and “Second”. “First” class inherited into the “Second” class and then create an object to “First” class using “Second” class constructor and access only “First” class variable through the created object. You cannot access “Second” class variable using the created object but two class constructors successfully run because we're creating an object to First class using the Second class constructor.

Type 5
  1. public class First   
  2. {  
  3.     public int varA;  
  4.     public First()   
  5.     {  
  6.         Console.WriteLine("First");  
  7.     }  
  8.   
  9. }  
  10. public class Second: First   
  11. {  
  12.     public int varB;  
  13.     public Second()   
  14.     {  
  15.         Console.WriteLine("Second");  
  16.     }  
  17. }  
  18.   
  19. class Program: First   
  20. {  
  21.     static void Main(string[] args)   
  22.     {  
  23.         First obj = new First();  
  24.         obj.varA = 10;  
  25.         //obj.varB = 20;        
  26.         Console.WriteLine("A Value:{0}", obj.varA);  
  27.         //Console.WriteLine("B Value:{0}", obj.varB);  
  28.         Console.Read();  
  29.     }  
Sample Output

First
A Value:10


Explanation

In Type 5, we have two classes “First” and “Second”. “First” class inherited into the “Second” class, then create an object to “First” class using the “First” class constructor and access only the “First” class variable through the created object. Here run only the first class constructors.

Type 6
  1. public class First   
  2. {  
  3.     public int varA;  
  4.     public First()   
  5.     {  
  6.         Console.WriteLine("First");  
  7.     }  
  8.   
  9. }  
  10. public class Second   
  11. {  
  12.     public int varB;  
  13.     public Second()   
  14.     {  
  15.         Console.WriteLine("Second");  
  16.     }  
  17. }  
  18.   
  19. class Program: First   
  20. {  
  21.     static void Main(string[] args)   
  22.     {  
  23.         Second obj = new Second();  
  24.         //obj.varA = 10;  
  25.         obj.varB = 20;  
  26.         //Console.WriteLine("A Value:{0}",obj.varA);  
  27.         Console.WriteLine("B Value:{0}", obj.varB);  
  28.         Console.Read();  
  29.     }  
  30. }  
Sample Output

Second
B Value:20


Explanation

In Type 6, we have two classes “First” and “Second”. “First” class is not  inherited into the “Second” classes. Create object to “Second” class using “Second” class constructor and access only “Second” class variable through the created object because it did not inherit “First” class to “Second” class. Here run “Second class” constructor.

Type 7
  1. public class First   
  2. {  
  3.     public int varA;  
  4.     public First()   
  5.     {  
  6.         Console.WriteLine("First");  
  7.     }  
  8.   
  9. }  
  10. public class Second: First   
  11. {  
  12.     public int varB;  
  13.     public Second(string varSecond)   
  14.     {  
  15.         Console.WriteLine(varSecond);  
  16.     }  
  17. }  
  18.   
  19. class Program: First {  
  20.     static void Main(string[] args)   
  21.     {  
  22.         Second obj = new Second();  
  23.         obj.varA = 10;  
  24.         obj.varB = 20;  
  25.         Console.WriteLine("A Value:{0}", obj.varA);  
  26.         Console.WriteLine("B Value:{0}", obj.varB);  
  27.         Console.Read();  
  28.     }  
  29. }  
Sample Output

This program does not run and displays an error.

Explanation

In Type 7, we have two classes “First” and “Second”. “First” class inherited into the “Second” class and then create an object to “Second” class using “Second” class constructor. While creating an object to “Second” class it will display an error because class “Second” has a parameter constructor but while creating an object we did not passed not passed a parameter value, so it displays an error. If we pass parameter like the following mentioned snippet, then it would run correctly.
 
Second obj= new Second(“Second”);

Note:

This type of concept is often asked in an interview.

Type 8
  1. public class First   
  2. {  
  3.     public int varA;  
  4.     public First(string varFirst)   
  5.     {  
  6.         Console.WriteLine(varFirst);  
  7.     }  
  8.   
  9. }  
  10. public class Second: First   
  11. {  
  12.     public int varB;  
  13.     public Second()   
  14.     {  
  15.         Console.WriteLine("Second");  
  16.     }  
  17. }  
  18.   
  19. class Program: First   
  20. {  
  21.     static void Main(string[] args)   
  22.     {  
  23.         Second obj = new Second();  
  24.         obj.varA = 10;  
  25.         obj.varB = 20;  
  26.         Console.WriteLine("A Value:{0}", obj.varA);  
  27.         Console.WriteLine("B Value:{0}", obj.varB);  
  28.         Console.Read();  
  29.     }  
  30. }  
Sample Output

This program does not run and displays an error.

Explanation

In Type 8, we have two classes “First” and “Second”. “First” class inherited into the “Second” class. After that create object to “Second” class using “Second” class constructor. While creating an object to “Second” class, error comes because class “First” has a parameter constructor but while creating an object we did not passed a parameter value,  so it displays and error in the “Second” class constructor: “First does not contains a constructor that takes 0 arguments ”.

The following will display error: “First does not contains a constructor that takes 0 arguments ” and “Second does not contains a constructor that takes 1 arguments ”.
  1. Second obj= new Second("Second");  
Can avoid error if constructor use same base and derived class signature.

Note:

This type of concept is often asked in interviews.

Conclusions

Above mentioned scenarios are generally asked in interviews. Inheritance helps in learning different scenarios with constructors and access modifiers.

Up Next
    Ebook Download
    View all
    Learn
    View all