0

I know that in c# a reference variable of parent class pointing to a child class object invoke the overridden method of child class object and hidden method of the base class object. In the runtime, the base class reference variable will figure out the actual type of the object, so it calls the overridden version. If the reference variable can check for the actual object, then why in case of hidden method it invokes the base class method?

  • 3
    Does this answer your question? [What is polymorphism, what is it for, and how is it used?](https://stackoverflow.com/questions/1031273/what-is-polymorphism-what-is-it-for-and-how-is-it-used) & [new keyword in method signature](https://stackoverflow.com/questions/1014295/new-keyword-in-method-signature) & [Difference between new and override](https://stackoverflow.com/questions/1399127/difference-between-new-and-override) –  May 09 '21 at 18:55
  • You should first get the terminology right. Your question doesn't make sense. – PMF May 09 '21 at 18:56
  • Could you please help me by specifying where I am getting wrong with my technical words? – Samin Sadman Shad May 09 '21 at 18:59
  • 3
    Because a `new` method (whether explicitly declared as such or only implicitly so) doesn't participate in virtual method resolution, it's really that simple. Note that there is no "figuring out" going on when a virtual method is invoked -- the type of the object is not explicitly determined as such, instead the virtual method is called according to the method table of the class. An override will just write a new method pointer there appropriate to the derived class. A method hiding a virtual method is not slotted into the method table of the base class, so it's never invoked. – Jeroen Mostert May 09 '21 at 18:59
  • @JeroenMostert thanks for clearing the concept by introducing 'method table'. I didn't know about its operation during method invocation. If I'm not wrong, is it like an override will include a new method pointer to the table of the base class ? – Samin Sadman Shad May 09 '21 at 19:21
  • 1
    [C# Virtual method tables](https://stackoverflow.com/questions/2413483/virtual-method-tables) & [Virtual method table](https://en.wikipedia.org/wiki/Virtual_method_table) & [VTable](https://learn.microsoft.com/visualstudio/debugger/debug-interface-access/vtable) –  May 09 '21 at 19:21

0 Answers0