There are basically two types of polymorphism in c# i.e.
Dynamic/Runtime/ Late Binding
Static- Static polymorphism is also called as Compile Time polymorphism. In Static polymorphism methods are overloaded with same name but having different signatures.So it is called as method overloading.
– In case of overloading methods whenever we call a method for that method call based on parameter if identifies which method should be executed and binds the method calls its method definition and method executed in runtime.
Dynamic-Dynamic polymorphism is also called as Run Time polymorphism. In this type of polymorphism methods have the same name, same signature but different in the implementation.
In Dynamic polymorphism methods are overridden so it also called as method overriding.
During run time, Method overriding can be achieved by using inheritance principle and using “virtual” and “override” keyword. so this type of polymorphism can also be called as late binding.
In late binding compiler doesn’t know what kind of methods it has to call and which can be achieved only during the run time.so it is called as run time polymorphism.
Polymorphism can be implemented in object oriented programming language using three different approach:
Overloading is again of 3 types-
- Method Overloading
- Constructor Overloading
- Operator Overloading
- Its an approach of defining multiple methods under a class with the same name by changing their signature.
- changing the signature in the sense, we can change the numbers of parameter being passed to method , type of parameter being pass to the method , order of parameter.
- Generally we overload constructor of a class so that variable of that class can be initialized either with default values with the help of parameter less constructor or can be initialized with the given values that are passed as parameter to the constructor while creating the object .
- Parameter less constructor also known as default constructors, they will initialize variable of a class with the default value.
- This is something very much similar to the concept of method overloading which allows to define multiple behaviors to an operator.
- In method overloading the behavior of method changes according to the type of parameter being passed to the method , whereas in the operator overloading the behavior of an operator changes according to the operands types between which we use the operator.
- ‘+’ is an overloaded operator which can be used both for addition as well as concatenation also. it works as a addition operator when used between numeric operands and works as a concatenation operator when used between string and numeric operands.
Example: + is already overloaded operator under the base class.
- <type> : implies the type of result we are expecting when the operator is used between two operands.
- <operand type definition>: in the sense the type of operands between which we want to use the operator.
- operator: is the name of the method that can naot be changed.
- <opt>: iplies the operator we want to overload.
- If at all parent classes method is redifined or reimplemented under the child class , exactly with the same name & signature , we call it as method overriding.
- Method overriding is possible only in derived classes, but not within the same class.
- When derived class needs a method with same signature as in base class, but wants to execute different code than provided by base class then method overriding will be used.
- To allow the derived class to override a method of the base class, C# provides two options,virtual methods and abstract methods.
- Method overriding in c# is achieved through inheritance.
public virtual void show() // overridable
public override void show() // overriding
Example 2:implementing abstract method