Before knowing Run-time polymorphism, let's explore 'What Polymorphism is?'
This is the ability for different classes to provide different implementations of same method. Also, the 'polymorphism' means we can have multiple classes that can be used interchangeably to use implementation of specific method in different ways.
There are 2 types of Polymorphism:
> Run-time polymorphism (dynamic binding , late binding)
> Compile time polymorphism (early binding)
1). Run-time polymorphism (Defined):
This is also known as lazy load, late binding, dynamic binding, overriding. And this can be achieved by 4 approaches as below:
- Method overriding ( Base class functionalities can be used by derived classes or can be overridden as needed )
- Property overriding ( Base class properties can be used by derived classes or can be overridden as needed )
- Using Delegate ( With delegates a polymorphic data structure can be created, by assigning desired function to delegate at run-time )
- Using Interface ( Different classes can implement same interface in different ways )
2). Compile-time polymorphism (Defined):
This is also known as early binding, static binding & overloading. And this can be achieved by 3 approaches as below:
- Method overloading ( Different implementation of same method which are differentiated with their signatures )
- Operator overloading ( Different implementation of same operator which are differentiated with their signatures )
- Indexer overloading ( Different implementation of an indexer which are differentiated with their signatures )
** Note **
Why we have choosen only these Methods, Operators and Indexers (and not 'Properties') for 'overloading'? Because, they receive parameters, based on which there can be multiple overloads created for them. Whereas, a Property cannot be overeloaded, as it doesn't receive parameters.
Hope, this helped.