Tuesday, March 16, 2010

Polymorphism in C#

so here's a basic question on Polymorphism in C#, i found at SO. looking for the basics these days.

class A {
public virtual void Method1(){}

public void Method2() {
Method1();
}
}

class B:A {
public override void Method1() { }
}

class main {
A myobject
= new B();
myobject.Method2();
}

So which function gets called?
B.Method1();

gets called because it properly overrides the virtual method A.Method1();

No comments: