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