As the same name suggests 'Poly' (many) 'morphos' (forms) an object existing in different forms. In our Programming environment we call it as an "Entity existing in many forms". Resilence to change is the best example of polymorphism. In OOPs we express polymorphism as " One Interface, Multiple Functions".
Talking about kinds of Polymorphism, they are classified into two basic forms
1. Static Polymorphism or compile time polymorphism
2. Dynamic Polymorphism or run time polymorphism.
In our programming environment we can see the importance of polymorphism by creating more than one function in a class with the same name but differing them with the parameter declaration, i.e method overloading is one of the feature where we see the implementation of polymorphism.
example: a woman who can be a wife, a daughter,a sister,a mother etc.... is an example of polymorphism.Static Polymorphism:
C# uses two approaches to implement static polymorphism.....
"The Mechanism of linking a function to a class object at compile time is called static polymorphism or early binding. "
1. Function overloading.
2. Operator Overloading.
1. Function Overloading:
Process of defining a function multiple times with same name inside a single class by changing the type of parameters, sequence of parameters, no of parameters.
additionFunction(int n1,int n2);2. Operator Overloading:
additionFunction(float n1, float n2);
Here the return type of the functions can be same or they can differ.
/*Dynamic Polymorphism:
*/
In Dynamic polymorphism the decision about function execution is made at runtime. Dynamic polymorphism is more useful as compared to static polymorphism because dynamic polymorphism gives much flexibility for manipulating objects.
"The mechanism of linking of a function with an object at runtime is called Dynamic Polymorphism of late binding. "C# uses two approaches to implement dynamic polymorphism,
1. Abstract classes .
2. Virtual Functions.
1. Abstract classes:
These are special type of base classes that consists of abstract class members. we can have abstract classes as well as abstract methods, and any class that contains one or more abstract methods must also be declared abstract. To declare a class abstract, you simply use the abstract keyword in front of the class keyword.
We cant create objects for abstract classes i.e ( classnameobj=new classname ), this kind of declaration is not possible for abstract classes.
We can create object references for abstract classes i.e (classname ref; ).
We cant declare abstract static methods and abstract constructors.
2. Virtual Functions:
These are the functions which do not really exists, but appear to be present in some parts of the program.
Labels: C# Programming