Click Here to View Practical Example
There are two types of polymorphism
There are two types of polymorphism
- Compile Time(Early binding or Overloading or static binding)
- Run Time (Late binding or Overriding or dynamic binding)
Compile time polymorphism means we will declare methods with same name but different signatures (Parameter)because of this we will perform different tasks with same method name. This compile time polymorphism also called as early binding or method overloading.
Syntax
public void NumbersAdd(int a, int b)
{
Console.WriteLine(a + b);
}
public void NumbersAdd(int a, int b, int c)
{
Console.WriteLine(a + b + c);
}
Method Overloading or compile time polymorphism means same method names with different signatures (different parameters)
Run time polymorphism or method overriding means same method names with same signatures.
In this run time polymorphism or method overriding we can override a method in base class by creating similar function in derived class this can be achieved by using inheritance principle and using “virtual & override” keywords.
In Base Class - Method is declared virtual and in derived class we override the same method
Syntax
//Base Class
public class Bclass
{
public virtual void Sample1()
{
Console.WriteLine("Base Class");
}
}
// Derived Class
public class DClass : Bclass
{
public override void Sample1()
{
Console.WriteLine("Derived Class");
}