C# program to show polymorphism using virtual and override keyword

Category > CSHARP || Published on : Saturday, November 14, 2020 || Views: 490 || show polymorphism using virtual and override keyword


Here Pawan Kumar will explain how to show polymorphism using virtual and override keyword

using System;
using System.Collections.Generic;
using System.Text;
namespace ConsoleApplication17
{
    class A
    {
        public virtual void Show()
        {
            Console.WriteLine("Show A");
        }
        public virtual void Fun()
        {
            Console.WriteLine("Fun A");
        }
    }
    class B : A
    {
        public override void Show()
        {
            Console.WriteLine("Show B");
        }
        public void Call()
        {
            Console.WriteLine("Call B");
        }
    }
   class Demo
    {
        static void Main(string[] args)
           {
            A a = new A();
            Console.WriteLine("***X***");
            a.Show();
            a.Fun();
            //a.Call(); // call() not accessible by A's abject
            B b = new B();
            Console.WriteLine("***Y***");
            b.Show();
            b.Fun();
            b.Call();
            a = b;
            Console.WriteLine("***when(a=b)***");
            a.Show();
            a.Fun();
            // a.Call(); // we cant call call() bcozz it is not defined by A class
           Console.ReadLine();
       }
    }
}