Monday, September 26, 2016

C# - Types of Polymorphism in C#.Net with Real Time Example

Polymorphism means many forms (ability to take more than one form). In Polymorphism poly means “multiple” and morph means “forms” so polymorphism means many forms.

1)Compile Time Polymorphism
2)Run Time Polymorphism

Compile Time Polymorphism: Compile time polymorphism means we will declare methods with same name but different signatures in the same class.
How to achieve them as shown below:
Example:
public class Overloading
    {
        //diff no of paramteres
        public int Method(int a)
        {
            return a;
        }
        public int Method(int a , int b)
        {
            return a + b;
        }

        // diff types of parameters
        public float Method(float a)
        {
            return a;
        }

        //diff order of parameters
        public void Method(int b, float a)
        {
        }
        public void Method(float a, int b)
        {
        }

    }
Run Time Polymorphism: Run time polymorphism also called as late binding or method overriding or dynamic polymorphism. Run time polymorphism or method overriding means same method names with same signatures in different class.








How to achieve run time polymorphism: In base class if we declare methods with virtual keyword then only we can override those methods in derived class using override keyword.



0 comments :

Post a Comment