Runtime polymorphism/Overriding with example program cod in a c# code
Runtime polymorphism/Overriding with example program cod in a c# code
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
VulpesPosted Jul 10, 2012, 10:51 AM
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: