Polymorphism using C#

This blog defines the Working of Polymorphism.

In This multi level inheritance also used.

Definition of class  A

public class A

{

    public void Print()

    {

        Console.WriteLine("Printing A");

    }

    public virtual void Write()

    {

        Console.WriteLine("Writing A");

    }

}
 
Class B is inherited from class A, defined as follows
 

public class B : A

{

    public void Print()

    {

        Console.WriteLine("Printing B");

    }

    public override void Write()

    {

        Console.WriteLine("Writing B");

    }

}

 
Similarly Class c is inherited from class A
 
Class D is inherited from class A
 

    public class D:C

    {

      public void Print()

        {

            Console.WriteLine("Printing D");

            

        }

        public override void Write()

        {

           base.Write();

            Console.WriteLine("Writing D");

        }

    }

    Main function () 

    public class Program

    {

       public static void Main(string[] args)

        {

            A a = new A();

            a.Print();

            a.Write();

            A b = new B();

            b.Print();

            b.Write();

            A c = new C();

            c.Print();

            c.Write();

            A d = new D();

            d.Print();

            d.Write();

            Console.ReadKey();

        }

    }

 
The output screen shot is as follows.

Image-1.jpg