Hi
Can someone help me straighten out my train of thought... I want to
know what actually goes on behind the scene..step by step.
Here is the code:
-----------------
using System;
namespace Test
{
public class MyBase
{
public virtual string Meth1()
{
return "MyBase-Meth1";
}
public virtual string Meth2()
{
return "MyBase-Meth2";
}
}
class MyDerived : MyBase
{
public override string Meth1()
{
return "MyDerived-Meth1";
}
public new string Meth2()
{
return "MyDerived-Meth2";
}
public static void Main()
{
MyDerived mD = new MyDerived();
MyBase mB = (MyBase) mD;
System.Console.WriteLine(mD.Meth1()); // Displays: MyDerived-Meth1
System.Console.WriteLine(mB.Meth1()); // Displays: MyDerived-Meth1
System.Console.WriteLine(mB.Meth2()); // Displays: MyBase-Meth2
System.Console.WriteLine(mD.Meth2()); // Displays: MyDerived-Meth2
}
}
}
-------
Here is what I think is currently happening (please correct me if I'm
wrong).
Question 1) When mD.Meth1() is called, it checks the memory locaton in
the heap contained in mD and goes to that location. Now, it finds the
MyDerived object.
**HERE IS WHERE MY CONFUSION BEGINS**
What does it actually do? Does it do the following:
PATH 1 - Go into MyDerived and realize it's inheriting MyBase. It then
goes to MyBase and finds Meth1() and notice it to be a virtual
function. It comes back out of MyBase and goes back to MyDerived and
notice that the function has been correclty overriden and uses that
function.
OR
PATH 2 - Go into MyDerived and does not even bother going into MyBase.
It notices that it has that function in it and it's overridding the
base function and calls it.
Quesiton 2) When mB.Meth2() is called, since the object really being
used is still MyDerived, how come MyDerived.Meth2() isn't called? The
new function obviously hides it, and the object being worked on is
MyDervived...not MyBase.
Why does it take PATH 1 and go into MyBase and calls that function when
the function has a keyword NEW which hides it?
Please help!!!