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

Sabarimahesh
Posted by Sabarimahesh under C# category on | Points: 40 | Views : 2367
creating an object of Derived class DerivedClass and storing its reference in the reference variable bc of type BaseClass. This is valid in C#.


class Program
{
static void Main(string[] args)
{
Baseclass bc;
bc = new Baseclass();
bc.Show();
bc = new DerivedClass();
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");
}
}



Output



Baseclass::Show
Baseclass::Show


bc is a reference of type Baseclass, the function Show() of class Baseclass will be invoked, no matter whom bc refers to.

Comments or Responses

Login to post response