Test Polymorphism Virtual Functions : Override Virtual « Class Interface « C# / C Sharp






Test Polymorphism Virtual Functions

/*
A Programmer's Introduction to C# (Second Edition)
by Eric Gunnerson

Publisher: Apress  L.P.
ISBN: 1-893115-62-3
*/
// 01 - Object-Oriented Basics\Polymorphism and Virtual Functions
// copyright 2000 Eric Gunnerson
using System;

public PolymorphismVirtualFunctions
{
    public static void CallPlay(MusicServer ms)
    {
        ms.Play();
    }
    public static void Main()
    {
        MusicServer ms = new WinAmpServer();
        CallPlay(ms);
        ms = new MediaServer();
        CallPlay(ms);
    }
}
public abstract class MusicServer
{
    public abstract void Play();
}
public class WinAmpServer: MusicServer
{
    public override void Play() 
    {
        Console.WriteLine("WinAmpServer.Play()");
    }
}
public class MediaServer: MusicServer
{
    public override void Play() 
    {
        Console.WriteLine("MediaServer.Play()");
    }
}


           
       








Related examples in the same category

1.Polymorphism
2.Virtual keyword can be used to start a new inheritance ladder
3.Virtual and overloadVirtual and overload
4.Class hierarchy: override and virtual
5.Use virtual methods and polymorphismUse virtual methods and polymorphism
6.Demonstrate a virtual methodDemonstrate a virtual method
7.When a virtual method is not overridden, the base class method is usedWhen a virtual method is not overridden, 
   the base class method is used
8.illustrates polymorphismillustrates polymorphism
9.Demonstrates the use of a virtual method to override a base class methodDemonstrates the use of a virtual method to override a base class method
10.Demonstrates the use of a virtual property to override a base class propertyDemonstrates the use of a virtual property to override a base class property
11.Method override 3Method override 3