Showing posts with label Virtual Classes. Show all posts
Showing posts with label Virtual Classes. Show all posts

Virtual Classes

Methods, properties, and indexers can be virtual, which means that their implementation can be overridden in derived classes.
The example: using System;
class CAbc
{
public virtual void F() { Console.WriteLine("CAbc.F"); }
}
class CBcd: CAbc
{
public override void F() {
base.F();
Console.WriteLine("CBcd.F");
}
}
class Test
{
static void Main() {
CBcd b = new CBcd();
b.F();
CAbc a = b;
a.F();
}
}
This shows a class CAbc with a virtual method F, and a class CBcd that overrides F. The overriding method in B contains a call base.F() which calls the overridden method in A.