Thursday, July 12, 2007

Polymorphism Example 3

public class A
{
public void PrintA()
{
Console.WriteLine("We are in class A");
}
}
public class B : A
{
public void PrintB()
{
Console.WriteLine("We are in class B");
}
}
class Program
{
static void Main(string[] args)
{
A a = new A();
a.writeA();
Console.ReadKey();
// Following code will give RUNTIME error.
B b = (B)a;
b.writeA();
b.writeB();
Console.ReadKey();
////////////////////////////////////////////
B bb = new B();
bb.writeA();
bb.writeB();
Console.ReadKey();
A aa = (A)bb;
aa.writeA();
Console.ReadKey();
}
}

No comments: