Saturday, 27 February 2016

C#.NET --- POLYMORPHISM

POLYMORPHISM 

Polymorphism is a concept of use a single method  we can provides different operations is known as polymorphism,

In general poly means many and morphism means form i.e., many forms for a single method .

Advantage :  

We can assigns some additional work to the function apart from the existing work.
We can provide different implementation to the same function or method

Suppose if a function add() do addition two number but we want addition three number with the name of add() function we can do by again writing it.

Polymorphism can be classified into two types :

1. Static Polymorphism
2. Dynamic Polymorphism


Static Polymorphism :

static polymorphism  is the polymorphism which occurs at compile time. the deciding choosing a method is done at compile time only. It is also called early  binding.

By using Function Overloading we can achieve it.

Dynamic Polymorphism :

dynamic polymorphism is the type of polymorphism which occurs at runtime only, that is choosing of which is method is decided at runtime  or execution time. It is also called late binding.

By using function overriding we can achieve dynamic polymorphism.


Function Overloading 

The mechanism of  providing different implementation to same function with different signature is called function overloading. It is a static polymorphism

we can write two or more functions with different signature. Here signature means

1. number of arguments
2. type of arguments
3. return type of function

If we write any two or more functions with same name and by changing number of arguments , type of arguments or return type of  arguments

Ex:






































Here in a same class we have wrote 3 add functions of different of signature. The compiler detects which type of function is suitable based on given parameter at compiler time.

Outpur is :





-----------------------------------------------------------------------------------------------------------


Function Overriding :

The mechanism of  providing a same function name  which is base class can write  with different implementation with same signature in derived class is called as function overriding

While doing inheritance if we want to provide different implementation for base class method we can override method by writing same function with same signature. the method choosing done at runtime 






No comments:

Post a Comment