Runtime polymorphism/Overriding with example program cod in a c# code
1 Answer
Know the answer? Post it. Somebody with the same question will find it here.
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend4days leftRegister
VulpesRank #55 · 29.2K reputationPosted Jul 10, 2012, 6:51 AM EDT
using System;
class Parent
{
public virtual void Method(string s)
{
Console.WriteLine("Parent.Method called with an argument of {0}", s);
}
}
class Child : Parent
{
public override void Method(string s)
{
Console.WriteLine("Child.Method called with an argument of {0}", s);
}
}
class Test
{
static void Main()
{
Parent p = new Parent();
p.Method("Hello"); // Parent's version called because 'p' refers to a Parent object
p = new Child();
p.Method("Goodbye"); // Child's version is called because 'p' refers to a Child object
Console.ReadKey();
}
}
The output is: