Overriding is a principle that allows you to change the functionality of a method in a child class.
For Ex.
//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 Inherit test class in test2 at that time compiler knows that there is a virtual method in parent class.
Then why Method overring is runtime bound and not compile time bound?
It is run-time binding (if that is even the right phrasing – I’m not convinced) because even with a
test2variable you could actually have:here the variable is a
test2, but the object is atest3. You could argue that maybe if it wassealedetc, but actually even non-virtual instance (non-static) methods go through the callvirt process. It works well, and is very fast. Additionally, the callvirt opcode has the necessary null-check, which means your code doesn’t (under the bonnet) have to constantly check for nulls (which would be necessary if it was static-call)The only exception here is
structs which override anobjectmethod; the following is a static call: