Overridding
What is mean overridding?With Example
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 attend5days leftRegister
VulpesRank #55 · 29.2K reputationPosted Nov 12, 2014, 1:25 AM EST
Joginder BangerRank #360 · 7K reputationPosted Nov 12, 2014, 1:35 AM EST
The C# language provides the override modifier for this purpose.
This keyword specifies that a method replaces its virtual base method.
using System;
class A
{
public virtual void Y()
{
// Used when C is referenced through A.
Console.WriteLine("A.Y");
}
} class B : A { public override void Y()
{ // Used when B is referenced through A.
Console.WriteLine("B.Y");
}
}
class C : A
{ public void Y()
// Can be "new public void Y()"
{
// Not used when C is referenced through A.
Console.WriteLine("C.Y");
}
}
class Program {
static void Main() {
// Reference B through A.
A ab = new B(); ab.Y();
// Reference C through A.
A ac = new C(); ac.Y();
}
}
Result B.Y A.Y
rgowthamraj ramajayam51 reputationPosted Nov 12, 2014, 2:37 AM EST