Why method overriding is a run-time limit

Overriding is a principle that allows you to change the functionality of a method in a child class.

For example.

 //Overriding public class test { public virtual getStuff(int id) { //Get stuff default location } } public class test2 : test { public override getStuff(int id) { //base.getStuff(id); //or - Get stuff new location } } 

When we test Inherit class in test2, while the compiler knows that there is a virtual method in the parent class. Then why is method overflow a runtime rather than time compilation?

+4
source share
4 answers

This is a run-time binding (if it's even the right phrase - I'm not convinced), because even with the variable test2 you could have:

 test2 obj = new test3(); // imagine test3 inherits from test2 obj.getStuff(id); 

here the variable is test2 , but the object is test3 . You can argue that it may have been sealed , etc., but actually not even virtual instances of (non-static) methods go through the callvirt process. It works well and is very fast. In addition, the callvirt opcode has a null check, which means that your code does not (under the hood) have to constantly check for zeros (which would be necessary if it was a static call)

The only exception to this is the struct , which overrides the object method; The following call is static :

 int i = 1; string s = i.ToString(); 
+3
source

Consider this snippet.

 test Create() { return new test2(); } test a = Create(); a.getStuff(0); // which method is called? 

The compiler cannot know that the return value of the Create method is of type test2. He is clearly hidden.

You should look at it from the point of view of the compiler. Given that the compiler looks at the value as if it were of type test , how could it know to call the test2.getStuff method?

+1
source
 class BaseClass { public virtual void Test(){ Console.WriteLine("This is test of base class"); } } class DerivedClass : BaseClass { public override void Test(){ Console.WriteLine("This is test of derived class"); } } class DerivedClass1 : BaseClass { public override void Test(){ Console.WriteLine("This is test of derived class-1"); } } 

now if you use

 BaseClass b = new DerivedClass(); b.Test(); // This will derived class method 

Suppose an object created by another method depends on the condition, but they are all derived from BaseClass

 public BaseClass GetObject(int i) { if(i==1) return new DerivedClass(); if(i==2) return new DerivedClass1(); } BaseClass b = GetObject(1); b.Test(); // This will derivedclass method BaseClass b = GetObject(2); b.Test(); // This will derivedclass1 method 

Thus, the whole thing depends on the value of i , which can be determined at run time and at run time, depends on the type of binding b in accordance with this test method call.

+1
source

Just to add to the other answers everything that the compiler cannot understand at compile time, it forces it to execute at run time, emitting the appropriate code that makes the material at run time or using other methods.

0
source

All Articles