Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You have to make the method <a href="http://msdn.microsoft.com/en-us/library/9fkccyh4%28v=vs.80%29.aspx" rel="nofollow noreferrer">virtual</a> and you have to override the function in the child class, in order to call the method of class object you put in parent class reference.</p> <pre><code>public class Person { public virtual void ShowInfo() { Console.WriteLine("I am Person"); } } public class Teacher : Person { public override void ShowInfo() { Console.WriteLine("I am Teacher"); } } </code></pre> <h2>Virtual Methods </h2> <blockquote> <p>When a virtual method is invoked, the run-time type of the object is checked for an overriding member. The overriding member in the most derived class is called, which might be the original member, if no derived class has overridden the member. By default, methods are non-virtual. You cannot override a non-virtual method. You cannot use the virtual modifier with the static, abstract, private or override modifiers, <a href="http://msdn.microsoft.com/en-us/library/9fkccyh4%28v=vs.80%29.aspx" rel="nofollow noreferrer">MSDN</a>.</p> </blockquote> <h2>Using New for Shadowing</h2> <p>You are using new key word instead of override, this is what new does</p> <ul> <li><p>If the method in the derived class is not preceded by new or override keywords, the compiler will issue a warning and the method will behave as if the new keyword were present.</p></li> <li><p>If the <strong>method in the derived class is preceded with the new keyword, the method is defined as being independent of the method in the base class</strong>, This <a href="http://msdn.microsoft.com/en-us/library/6fawty39%28v=vs.80%29.aspx" rel="nofollow noreferrer">MSDN article</a> explains it very well.</p></li> </ul> <h2>Early binding VS Late binding</h2> <p>We have early binding at compile time for normal method (not virtual) which is the currrent case the <strong>compiler will bind call to method of base class that is method of reference type (base class) instead of the object is held in the referece of base class i.e. derived class object</strong>. This is because <code>ShowInfo</code> is not a virtual method. The late binding is performed at runtime for (virtual / overridden method) using <a href="http://en.wikipedia.org/wiki/Virtual_method_table" rel="nofollow noreferrer">virtual method table</a> (vtable). </p> <blockquote> <p>For a normal function, the compiler can work out the numeric location of it in memory. Then it when the function is called it can generate an instruction to call the function at this address.</p> <p>For an object that has any virtual methods, the compiler will generate a v-table. This is essentially an array that contains the addresses of the virtual methods. Every object that has a virtual method will contain a hidden member generated by the compiler that is the address of the v-table. When a virtual function is called, the compiler will work out what the position is of the appropriate method in the v-table. It will then generate code to look in the objects v-table and call the virtual method at this position, <a href="https://stackoverflow.com/questions/484214/early-and-late-binding">Reference</a>.</p> </blockquote>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload