Run Time PolyMorphism Example---3 For Beginner??????

Sabarimahesh
Posted by Sabarimahesh under C# category on | Points: 40 | Views : 1219
class Program
{
static void Main(string[] args)
{
Baseclass bc;
bc = new Baseclass();
bc.Show();
bc = new DerivedClass();
bc.Show();
bc = new DerivedClass2();
bc.Show();
Console.ReadLine();

}

}

class Baseclass
{
public void Show()
{
System.Console.WriteLine("Baseclass::Show");
}
}

class DerivedClass : Baseclass
{
new public void Show()
{
System.Console.WriteLine("DerivedClass::Show");
}
}

class DerivedClass2 : Baseclass
{
new public void Show()
{
System.Console.WriteLine("DerivedClass2::Show");
}
}



Output


Baseclass::Show
Baseclass::Show
Baseclass::Show


it invokes the functions of the class that matches its Reference bc
bc contains the reference to a particular derived class object, then its supposed to invoke the function of that class

Comments or Responses

Login to post response