Program for Run Time Polymorphism using C Sharp

using System;

class Manager

{

     public virtual void Work()

     {

         Console.WriteLine(“Manager”);

     }

}

class Employee : Manager

{

     public override void Work()

     {

         Console.WriteLine(“Employee”);

     }

}

class Trainee : Employee

{

     public override void Work()

     {

         Console.WriteLine(“Trainee”);

     }

     public void Chess()

     {

         Console.WriteLine(“Playing chess”);

     }

}

class Program

{

    public static void Main()

    {

        Manager obj = new Employee(); //Late binding or Run time Polymorphism

        Manager obj1 = new Trainee(); //Late binding or Run time Polymorphism

        obj.Work(); // Upcasting or Co-variance or R.T.Polymorphism

        obj1.Work();// Upcasting or Co-variance or R.T.Polymorphism

        ((Trainee)obj1).Chess(); // Explicit conversion or Downcasting

    }

}

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.