Polymorphism

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:

"The Mechanism of linking a function to a class object at compile time is called static polymorphism or early binding. "

C# uses two approaches to implement static polymorphism.....

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);
additionFunction(float n1, float n2);

Here the return type of the functions can be same or they can differ.
2. Operator Overloading:

/*

*/

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 ( classname obj=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.

2 Comments:

  1. Unknown said...
    Good attempt at explaining polymorphism in C#, but unfortunately there are couple of misinformation and concepts would have been more clear with example or explanation. For example, there is no example of how an abstract class and its child classes can be used to achieve dynamic polymorphism. There is no mention of dynamic polymorphism using interface either. Generics should have been mentioned as a form of static polymorphism. Also, I feel equating static polymorphism with early binding and dynamic polymorphism with late binding are not right. The terms are not interchangeable as polymorphism and binding represent different concepts. You should rather say static polymorphism USES early binding and dynamic polymorphism USES late binding.
    Finally, your comment about return types of overloaded methods can be different is wrong, as C# does not allow so. It only allows the method parameter list to be different.
    vivek said...
    This comment has been removed by the author.

Post a Comment



Newer Post Older Post Home