Monday, 13 December 2010

Dynamic Binding

class A
{
 public virtualvoid WhoAreYou()
 {
  Console.WriteLine("I am an A");
 }
}
class B : A
{
 public overridevoid WhoAreYou()
 {
 Console.WriteLine("I am a B");
 }

A message invokes the method belonging to the dynamic typeof the receiver
A a = new B();a.WhoAreYou();// "I am a B"

Every method that can work with A can also work with B
void Use (A x)
{
x.WhoAreYou();
}
Use(new A());// "I am an A
"Use(new B());// "I am a B"

No comments:

Post a Comment