// static, dynamic types in C#, type casting using System; using System.Collections; using System.Collections.Generic; interface I { void f(); void g(); } class AA : I { public int x = 1; public void f() { Console.WriteLine("this is AA.f(), x is "+x); } public virtual void g() { Console.WriteLine("this is AA.g(), x is "+x); } } class BB : AA, I { public new int x = 2; // hides old intx // inherits f public new void f() { Console.WriteLine("this is BB.f(), x is "+x); } public override void g() { Console.WriteLine("this is BB.g(), x is "+x); } public int y = 4; public void h() { Console.WriteLine("this is BB.h(), y is "+y); } } public class inherit07 { public static void Main() { AA m = new BB(); // implicit type cast m.f(); // still prints 1 even with override!, unless interface is used m.g(); // m.h(); compile time error: h() not in static type interface ((BB)m).h(); // OK AA m2 = new AA(); m2.g(); // ok // ((BB)m2).h(); // runtime error // using the "object" supertype ArrayList A = new ArrayList(); //arraylist object A.Add(30); // adds an int A.Add(3.14); // adds a double A.Add("abc"); // adds a string foreach (object x in A) { Console.Write(x+"\t");} Console.WriteLine(""); // no problem //int y = 2 * A[0]; // compile time error int y = 2 * (int)A[0]; // OK // y = 2 * (int)A[1]; // runtime error }// Main }