Introduction

What is Polymorphism?

Polymorphism means same operation may behave differently on different classes.

Example of Compile Time Polymorphism

Example of Method Overloading:

class A1
{
void hello()
{
Console.WriteLine("Hello");
}
void hello(string s)
{
Console.WriteLine("Hello {0}",s);
}
}

Example of Run Time Polymorphism


Note: By default functions are not virtual in C# and so you need to write “virtual” explicitly. While by default in Java each function are virtual.
Example of Method Overriding:

Class parent
{
virtual void hello()
{
Console.WriteLine("A D Patel");
}
}
Class child : parent
{
override void hello()
{
Console.WriteLine("R A Patel");
}
}
static void main()
{
parent objParent = new child();
objParent.hello();
}
//Output
R A Patel