Implement Polymorphism through method overriding

Polymorphism means one name ,many forms .In this , We can implement by defining a method using the keyword Virtual in a base class and redefining the same method using the keyword override in the derived classes of the base class.

Following are the rules for implementing method overriding :

1)We can override only those methods which are declared with the keyword virtual in the base class .

2)Type of parameters and no of parameters of all overridden methods in the derived classes should match with the types of parameters and number of parameters of the method defined in the base class to which they are overriding .

Example :

using System;
using System.Collections.Generic;
using System.Text;
namespace Overriding_Ex
{
    class blogger
    {
        public virtual void Display()
        {
            Console.WriteLine("blogger");
        }
    }
    class Linuxtree : blogger
    {
        public override void Display()
        {
            Console.WriteLine("linuxtree");
        }
    }
    class cybergyaan : blogger
    {
        public override void Display()
        {
            Console.WriteLine("cybergyaan");
        }
        class program
        {
            static void Main(string[] args)
            {
                blogger b;
                b = new Linuxtree();
                b.Display();
                b = new cybergyaan();
                b.Display();
                Console.Read();
            }
        }
   }
}
Screenshots:

2010-05-24_011424

2010-05-24_111024

2010-05-24_112351

2010-05-25_003947

2010-05-24_112558

Download the Folder

download-button

Try it & Comment …

One Response to “Implement Polymorphism through method overriding”
  1. kannan

    Wow nice code… But pretty much complicated, may be the language i think try this same thing in perl or python wont take more than 5 lines.

Leave a Reply

*