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.
- namespace CLP1
- {
- public class A
- {
- public int M1(B b)
- {
- int r=b.M2(10,2);
- return r;
- }
- }
- public class B
- {
- public virtual int M2(int x,int y)
- {
- return x-y;
- }
- }
- }
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):
- namespace CLP2
- {
- public class K : B
- {
- public override int M2(int x, int y)
- {
- return x + y;
- }
- }
- }
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.create a class library project with the name CLP1 .
-
- 2.create a class A with method M1 as shown below .
- namespace CLP1
- {
- public class A
- {
- public int M1(B b)
- {
- int r=b.M2(10,2);
- return r;
- }
- }
- public class B
- {
- public virtual int M2(int x,int y)
- {
- return x-y;
- }
- }
- }
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):
- using CLP1 ;
- namespace CLP2
- {
- public class K : B
- {
- public override int M2(int x, int y)
- {
- return x + y;
- }
- }
- }
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):
- public class Test
- {
- public static void Main(string[] args)
- {
- Console.WriteLine("Enter 2 numbers ");
- Console.WriteLine("X=");
- int x=Int32.Parse(Console.ReadLine());
- Console.WriteLine("Y=");
- int y = Int32.Parse(Console.ReadLine());
- A a = new A();
- K k = new K();
- int res=a.M1(k);
- Console.WriteLine(res);
- }
- }
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.