An illustration of virtual and new methods in class A and B - Simplified. | Lecture 7 - slide 35 : 40 Program 2 |
using System; class A { public virtual void N(){Console.WriteLine("N in A");} public void P(){Console.WriteLine("P in A");} } class B: A{ public override void N(){Console.WriteLine("N in B");} public new void P(){Console.WriteLine("P in B");} } class Client { public static void Main(){ A aa = new A(), ab = new B(); B b = new B(); aa.N(); ab.N(); b.N(); // The dynamic type controls Console.WriteLine(); aa.P(); ab.P(); b.P(); // The static type controls } } |