Click here to Skip to main content
15,884,472 members
Articles / Programming Languages / C#
Tip/Trick

Overloading Is A Type Of Dynamic Polymorphism

Rate me:
Please Sign up or sign in to vote.
4.33/5 (4 votes)
24 May 2013CPOL2 min read 21.5K   67   5   6
An example of overloading dynamic polymorphism

Introduction

Polymorphism is the occurrence of something in different forms where 'poly' means many and ‘morphism’ means form.

It is an ability of an object to take different forms based on the situation.

Creating a method in the derived class with the same signature as a method in base class is called as method overriding.

Let’s see if overriding can bring run time polymorphism.

Background

Let me start my story first.

  • Bird can fly but cannot run.
  • Penguin is a bird which cannot fly but can run.

Requirements are to show:

  • What Bird does and does not do?
  • What penguin does and does not do?
  • What Bird and Penguin can do?

Using the Code

Based on the above story, let’s build our requirement.

Let me introduce you to three key words.

  • Virtual: The virtual key word is used to modify a method and allow it to be overridden in derived class, i.e., it says that I am the first version and the next version will get derived in the derived class.
  • Override: The override key word is use to declare new version of method that is derived in the base class.
  • New: The new keyword is used to hide the inherited method declared in the base class and modify it with new modifier, i.e. it hid the base class method and says that it is the complete new implementation of the method.

So you need to build the following structure:

Bird Base Class

C#
/// <summary>
///Base class
/// </summary>
public class Bird
{
    string Name { get; set; }
    public Bird()
    {
        Name = "Bird ";
    }
    public virtual void Fly()
    {
        Console.WriteLine(Name+"I can fly.");
    }
    public virtual void Run()
    {
        Console.WriteLine(Name+"I can not run.");
    }
}

Penguin Derived Class

C#
/// <summary>
/// derived class
/// </summary>
public class Penguin:Bird
{
    string Name { get; set; }
    public Penguin()
    {
        Name = "Penguin ";
    }
    public new void Fly()
    {
        Console.WriteLine(Name + "I can not fly");
    }
    public override void Run()
    {
        Console.WriteLine(Name+"I can Run");
    }
} 

We want what Bird and Penguin can do?

C#
Bird bird1 = new Penguin();

Let's look at the above code. We assign the penguin class to the variable of the bird class. Is it not the same as type casting?

C#
bird1.Run();
bird1.Fly(); 

Look at the above call as both the methods are virtual and object is of type cast from derived to base class object.

So the compiler will find the override method in the derived class. As virtual says that next version of same method is defined in the derived class and override says that it is the next version of the same method declared as virtual in base class. Compiler will reference the derived class Run() method instead of base class Run() method. But for base class fly() method, we have a complete new implementation for derived class fly() method denoted by the keyword New and compiler did not find any override implementation of it so compiler reference to base class fly() method.

Fine enough that we want.

C#
static void Main(string[] args)
{
    //using bird class
    Console.WriteLine("Creating bird object........");
    Bird bird = new Bird();
    bird.Run();
    bird.Fly();
    
    //using penguin class
    Console.WriteLine("Creating penguin object........");
    Penguin penguin = new Penguin();
    penguin.Run();
    penguin.Fly();
    
    //Assign penguin to bird
    Console.WriteLine("Creating penguin object and assign to bird........");
    Bird bird1 = new Penguin();
    bird1.Run();
    bird1.Fly();
    Console.Read();    
} 

We have the same method name in base and derived class and we are using object of base class having the same method name Run() with different implementation during run time call as dynamic polymorphism.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Web Developer Lionbridge
India India
Amey K Bhatkar, a “Microsoft .Net” Web Developer.
I am programmer by will and profession.
I have completed my MCA in 2011 and join software industry.
Presently I am working with Lion Bridge Technologies in Mumbai - India

Comments and Discussions

 
GeneralMy vote of 4 Pin
vijay venkatesh prasad N21-Aug-13 19:45
vijay venkatesh prasad N21-Aug-13 19:45 
QuestionOverloading dynamic polymorphism?? Pin
Cracked-Down22-Apr-13 1:48
Cracked-Down22-Apr-13 1:48 
AnswerRe: Overloading dynamic polymorphism?? Pin
Cracked-Down24-Apr-13 0:35
Cracked-Down24-Apr-13 0:35 
AnswerRe: Overloading dynamic polymorphism?? Pin
Amey K Bhatkar24-May-13 1:30
Amey K Bhatkar24-May-13 1:30 
GeneralRe: Overloading dynamic polymorphism?? Pin
Cracked-Down24-May-13 3:38
Cracked-Down24-May-13 3:38 
GeneralRe: Overloading dynamic polymorphism?? Pin
Amey K Bhatkar24-May-13 4:28
Amey K Bhatkar24-May-13 4:28 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.