Polymorphism is the ability to treat various objects in the same manner. "Poly" means many, and "morph" means forms.
In other words, one object has many forms or has one name with multiple functionalities. Polymorphism allows a class to have multiple implementations with the same name
Polymorphism is often referred to as the third pillar of object-oriented programming, after encapsulation and inheritance.
Types of Polymorphism
Static / Compile Time Polymorphism
Dynamic / Runtime Polymorphism
Compile-Time Polymorphism (Method Overloading Or early binding Or Static binding)
This type of polymorphism is achieved by function overloading or operator overloading.
A. Function Overloading
When there are multiple functions with the same name but different parameters, then the functions are said to be overloaded,
Functions can be overloaded by changing the number of arguments or/and changing the type of arguments or/and order of parameters.
The return type of the methods does not play any role in the method overloading.
Invoking Overloaded Methods
We can call the overloaded method by passing the exact parameter it requires in exact same sequence.
B. Operator Overloading
An Operator can give different outputs based on the type of operands that it is operating on. This is called Operator Overloading.
E.g., the operator ‘+’ can perform addition on two integers, while it can concatenate two strings. Thus the same operator can be used in 2 different ways.
Runtime Polymorphism (Late binding Or Dynamic polymorphism)
This type of polymorphism is achieved by Function / Method Overriding using inheritance.
The function call is resolved at runtime in runtime polymorphism.'
The process of re-implementing the superclass non-static, non-private, and non-sealed method in the subclass with the same signature is called Method Overriding in C#. The same signature means the name and the parameters (type, number, and order of the parameters) should be the same.
With method overriding, it is possible for the base class and derived class to have the same method name and the same signature.
Why in both cases the Child Class Show Method is Invoked?
Here, Class1 is the superclass and Class2 is the subclass. The point that you need to keep in mind is that the Super Class Reference Variable Can hold the Subclass object reference and but the reverse is not possible i.e. Sub Class Reference Variable can never hold the Super Class Object Reference. So, the Class1 reference variable can hold the Class2 object reference.