C# Virtual Function Members
In this chapter you will learn:
- What are C# Virtual Function Members
- Example for Virtual Function Members
- virtual methods from parent classes
- What is new versus virtual
- What are virtual properties and how to use virtual properties
Description
A function marked as virtual can be overridden by subclasses wanting to provide a specialized implementation.
Methods, properties, indexers, and events can all be declared virtual:
A subclass overrides a virtual method by applying the override modifier.
When overriding a method, the type signature of the method cannot be changed. A virtual method cannot be static or abstract.
Example
The following code shows how to use the virtual method
to provide the different implementation of the Area
.
using System;/*from www . j a v a2 s .com*/
class Shape{
public virtual double Area{
get{
return 0;
}
}
}
class Rectangle:Shape{
public int width;
public int height;
public override double Area{
get{
return width * height;
}
}
}
class Circle:Shape{
public int radius;
public override double Area{
get{
return 3.14 * radius * radius;
}
}
}
class Program
{
static void Main(string[] args)
{
Rectangle r = new Rectangle();
r.width = 4;
r.height = 5;
Circle c = new Circle();
c.radius = 6;
Console.WriteLine(r.Area);
Console.WriteLine(c.Area);
}
}
The output:
C# requires that the virtual and override must have the same signature, accessor and return type.
Example 2
When a virtual method is not overridden, the base class method is used.
using System; // w w w . jav a2s .c om
class BaseClass {
// Create virtual method in the base class.
public virtual void who() {
Console.WriteLine("who() in BaseClass");
}
}
class Derived1 : BaseClass {
// Override who() in a derived class.
public override void who() {
Console.WriteLine("who() in Derived1");
}
}
class Derived2 : BaseClass {
// This class does not override who().
}
class MainClass {
public static void Main() {
BaseClass baseOb = new BaseClass();
Derived1 dOb1 = new Derived1();
Derived2 dOb2 = new Derived2();
BaseClass baseRef; // a base-class reference
baseRef = baseOb;
baseRef.who();
baseRef = dOb1;
baseRef.who();
baseRef = dOb2;
baseRef.who(); // calls BaseClass's who()
}
}
The code above generates the following result.
Example 3
Consider the following class hierarchy: The differences in behavior between Overrider and Hider are demonstrated in the following code:
public class BaseClass
{/* ww w .ja va 2s.c om*/
public virtual void Foo() { Console.WriteLine ("BaseClass.Foo"); }
}
public class Overrider : BaseClass
{
public override void Foo() { Console.WriteLine ("Overrider.Foo"); }
}
public class Hider : BaseClass
{
public new void Foo() { Console.WriteLine ("Hider.Foo"); }
}
Overrider over = new Overrider();
BaseClass b1 = over;
over.Foo(); // Overrider.Foo
b1.Foo(); // Overrider.Foo
Hider h = new Hider();
BaseClass b2 = h;
h.Foo(); // Hider.Foo
b2.Foo(); // BaseClass.Foo
Example 4
using System;//from w ww . ja v a 2s.c o m
abstract public class Currency {
private decimal mEuroValue = 0M; // [Euro]
public abstract decimal CurrencyValue { get; set; }
public decimal EuroValue {
get {
return mEuroValue;
}
set {
mEuroValue = value;
}
}
}
public class MyMoney : Currency {
private static decimal mDMtoEuro = 1.96M;
public override decimal CurrencyValue {
get { return EuroValue * mDMtoEuro; }
set { EuroValue = value / mDMtoEuro; }
}
}
public class Dollar : Currency {
public decimal mDollartoEuroConversion;
public void InitDollar(decimal mDollartoEuro) {
mDollartoEuroConversion = mDollartoEuro;
}
public override decimal CurrencyValue {
get { return EuroValue * mDollartoEuroConversion; }
set { EuroValue = value / mDollartoEuroConversion; }
}
}
public class Class1 {
public static void Main(string[] strings) {
MyMoney dm = new MyMoney();
dm.CurrencyValue = 2.50M;
Console.WriteLine(dm.CurrencyValue);
Dollar dollar = new Dollar();
dollar.InitDollar(1.2M);
dollar.EuroValue = dm.EuroValue;
Console.WriteLine(dollar.CurrencyValue);
dollar.CurrencyValue = 2 * dollar.CurrencyValue;
dm.EuroValue = dollar.EuroValue;
Console.WriteLine(dm.CurrencyValue);
}
}
The code above generates the following result.
Next chapter...
What you will learn in the next chapter: