Hi Sunny,
The reason why you are getting the overridden version of the method, is
because you've created an instance of type B in memory, and you are saving
the the object reference in 'b', then copying that object reference to 'a'.
That means that 'a' and 'b' point to the same object in memory, so, you are
calling the overridden method, as opposed to the method in the base class.
You need to declare an instance of A, if you want to use it's methods, as
opposed to the subclass B's methods:
///
A a = new A( );
Console.WriteLine( a.F() );
///
-- Tom Spink
Sunny wrote:
Hi all,
The code as follow:
using System;
class A
{
public virtual void F()
{
Console.WriteLine("A.F");
}
}
class B : A
{
public override void F()
{
Console.WriteLine("B.F");
}
public override string ToString()
{
return "Class B: ToString()";
}
}
class Test
{
static void Main()
{
B b = new B();
A a = b;
a.F();
Console.WriteLine(a.ToString());
}
}
Why the output is:
B.F
Class B: ToString()
Thanks