Polymorphism and Virtual Functions : Abstract Class « Class « C# / CSharp Tutorial






using System;

public abstract class Clock {
    public abstract void Play();
}

public class RedClock: Clock {
    public override void Play() {
        Console.WriteLine("RedClock.Play()");
    }
}
public class BlueClock: Clock{
    public override void Play() 
    {
        Console.WriteLine("BlueClock.Play()");
    }
}
class MainClass
{
    public static void CallPlay(Clock ms)
    {
        ms.Play();
    }
    public static void Main()
    {
        Clock ms = new RedClock();
        CallPlay(ms);
        ms = new BlueClock();
        CallPlay(ms);
    }
}
RedClock.Play()
BlueClock.Play()








7.27.Abstract Class
7.27.1.Using Abstract Classes
7.27.2.Create an abstract class.
7.27.3.Override abstract method
7.27.4.Polymorphism and Virtual Functions
7.27.5.Define abstract class and abstract method
7.27.6.abstract class with three virtual methods