// properties of inheritance using System; interface I { void f(); void g(); } class AA : I { int x = 1; // default private public void f() {Console.WriteLine("this is AA.f, x is "+x); } public virtual void g() {Console.WriteLine("this is AA.g");} } class BB : AA, I // BB extends AA implements I { public int y = 2; public new void f() {Console.WriteLine("this is BB.f");} public override void g() {Console.WriteLine("this is BB.g, y is "+y);} public void h() { Console.WriteLine("this is BB.h"); } } class CC : AA, I { double z = 3.2144; /* // defines casting from CC object to BB object: public static explicit operator BB(CC m) { BB b = new BB(); b.y = (int)(m.z+0.5); // convert to int, rounding off. return b; } */ } public class inprop { public static void Main() { I a1; // static type of a1 a1 = new AA(); // dynamic type of a1 is AA AA a2 = new BB(); // static type of a2 is AA, dynamic type is BB a2.f(); // base a2.g(); // subclass BB a3 = new BB(); // not very interesting //BB b1 = new AA(); // not ok? compiler error AA a4 = new CC(); AA a5 = new AA(); BB b1 = null; //b1 = (BB)a5; // not compiler error, runtime error a5 is acutally AA object // What are all the valid static casts? a4 = b1; // a BB object is a AA object, no cast operator needed b1 = (BB)a1; // casting downwards: an AA static var may point to a BB object CC c1 = new CC(); //b1 = (BB)c1; // compiler error: a CC object cannot become a BB object // uncomment the public static explicit operator in class CC, and the above // will compile and work. }//main }