Click here to Skip to main content
Click here to Skip to main content

Overloading Is A Type Of Dynamic Polymorphism

, 24 May 2013
Rate this:
Please Sign up or sign in to vote.
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

/// <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

/// <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?

  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?

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.

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)

Share

About the Author

Amey K Bhatkar
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
Follow on   Twitter   Google+

Comments and Discussions

 
GeneralMy vote of 4 Pinmembervijay venkatesh prasad N21-Aug-13 19:45 
QuestionOverloading dynamic polymorphism?? [modified] PinmemberCracked-Down22-Apr-13 1:48 
What do you mean by Overloading dynamic polymorphism??
 
what is the difference between "Overloading dynamic polymorphism and polymorphism??
 
I find this article heading very confusing....

modified 22-Apr-13 8:07am.

AnswerRe: Overloading dynamic polymorphism?? PinmemberCracked-Down24-Apr-13 0:35 
AnswerRe: Overloading dynamic polymorphism?? PinprofessionalAmey K Bhatkar24-May-13 1:30 
GeneralRe: Overloading dynamic polymorphism?? PinmemberCracked-Down24-May-13 3:38 
GeneralRe: Overloading dynamic polymorphism?? PinprofessionalAmey K Bhatkar24-May-13 4:28 

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

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

| Advertise | Privacy | Mobile
Web02 | 2.8.140821.2 | Last Updated 24 May 2013
Article Copyright 2013 by Amey K Bhatkar
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid