Overriding in C# and Internals of virtual and Overriding

Overriding usually means changing the behavior of a base or parent class method in the child or derived class. Overriding is usually required when creating the frameworks for calling known methods present in unknown classes. In my experience i observed many people getting confused with overriding and also it is most frequently asked C# interview question and answers 


Explanation

Assume you are writing a class in a class library project (CLP1) whose name is A. Now your class A has a method M1 that needs to call a method present in an unknown class. Now assume developer 2 creates another class in the future that contains a method M2, but you don't know when the other developer will write the code.

Now you need to complete your code in such a way that when developer 2 completes his code for method M2 and both the projects are integrated your method M1 must be able to call method M2 present in a future class created by developer 2 in another class library project CLP2.

I am referring to the preceding explained situation as calling a known method present in an unknown class. Since while writing code in CLP1 you don't know the class name in which method M2 is present but still your code needs to call the method.

Sample Code

The code

1. Create a class library project with the name CLP1.

2. Create a class A with method M1 as shown below.

  1. namespace CLP1
  2. {      
  3.   public class A      
  4.   {           
  5.     public int M1(B b)          
  6.     {              
  7.        int r=b.M2(10,2);  
  8.        return r;          
  9.     }      
  10.   }      
  11.  public class B       
  12.  {          
  13.     public virtual int M2(int x,int y)          
  14.     {              
  15.        return x-y;          
  16.     }       
  17.  }  
  18. }  

Second developer's code (future code)

Create a class library project with the name CLP2 and write the following code (do not forget to first add a class library project reference CLP1 to CLP2):

  1. namespace CLP2  
  2. {  
  3.     public class K : B  
  4.     {  
  5.         public override int M2(int x, int y)  
  6.         {  
  7.             return x + y;  
  8.         }  
  9.     }  
  10. }  

Note: If you observe the code written by the second developer, the second developer is inheriting class K from B and overriding the M2 method.

What a virtual method is

A Virtual method is a low priority method whose behavior can be changed by overriding.

Testing Code (for understanding the calling of known methods present in an unknown classes)

Why overriding is required
 
Overriding is usually required when creating the frameworks for calling known methods present in unknown classes.
 
Explanation: assume you are writing a class in a class library project (CLP1) whose name is A . Now your class A has a method M1 that needs to call a method present in an unknown class. Now assume developer 2 creates another class in the future that contains a method M2, but you don't know when the other developer is writing the code. Now you need to complete your code in such a way that when developer 2 completes his code for the method M2 and both the projects are integrated your method M1 must be able to call method M2 present in a future class created by developer 2 in another class library project CLP2 .The preceding explained situation I am calling a known method present in an unknown class since, while writing code in CLP1, you don't know the class name in which method M2 is present but still your code needs to call the method.
 
Your Code: 
  1.   
  2. 1.create a class library project with the name CLP1 .  
  3.   
  4. 2.create a class A with method M1 as shown below .
  5. namespace CLP1  
  6. {  
  7.     public class A  
  8.     {  
  9.         public int M1(B b)  
  10.         {  
  11.             int r=b.M2(10,2);  
  12.             return r;  
  13.         }  
  14.     }  
  15.     public class B  
  16.     {  
  17.         public virtual int M2(int x,int y)  
  18.         {  
  19.             return x-y;  
  20.         }  
  21.     }  
  22. }   

Second developers code :

Create a class library project with the name CLP2 and write the following code (do not forget to add the first class library project reference CLP1 to CLP2):
 
  1. using CLP1 ;
  2. namespace CLP2  
  3. {  
  4. public class K : B  
  5. {  
  6. public override int M2(int x, int y)  
  7. {  
  8. return x + y;  
  9. }  
  10. }  
  11. }  
Note: If you observe the code written by the second developer, the second developer is inheriting class K from B and overriding the M2 method.
What a virtual method is: A virtual method is a low priority method whose behavior can be changed by overriding.


Testing Code (for understanding calling known methods present in unknown classes):
  1. public class Test  
  2. {  
  3.  public static void Main(string[] args)  
  4.  {  
  5.   Console.WriteLine("Enter 2 numbers ");  
  6.   Console.WriteLine("X=");  
  7.   int x=Int32.Parse(Console.ReadLine());  
  8.   Console.WriteLine("Y=");  
  9.   int y = Int32.Parse(Console.ReadLine());  
  10.   A a = new A();  
  11.   K k = new K();  
  12.   int res=a.M1(k);  
  13.   Console.WriteLine(res);  
  14.   }  
  15. }  
This code will work since the CLR can identify the parent class (B class) reference using the child class (K class) reference using down casting.

Question: what is down casting?
Answer: identifying a parent class object reference using a child class object reference.
The output here is 12 not 8 since the CLR will call the overriden method M2 present in Class K though the M1 method in class A that is calling virtual method M2 present in Class B.

Observation: Class A is not aware of Class K but still code b.M2(10,2) written in M1 of class A is calling class K's method. That means that Class A's M1 method is calling the M2 method present in Class K without knowing the class name in which it is present.
 
Program Execution mechanism (using RAM Architecture)

Observe the call routing from the virtual method to override the method in the screen shot given below.

overriding
For testing your technical skills you can use:

C# interview questions and answers