Overloading Is A Type Of Dynamic Polymorphism






4.33/5 (4 votes)
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 abird
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
andPenguin
can do?
Using the Code
Based on the above story, let’s build our requirement.
Let me introduce you to three key words.
Virtual
: Thevirtual
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
: Theoverride
key word is use to declare new version of method that is derived in the base class.New
: Thenew
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.