Monday 11 August 2014

Polymorphism in C#

Polymorphism:

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.
In polymorphism we will declare methods with same name and different parameters in same class or methods with same name and same parameters in different classes. Polymorphism has ability to provide different implementation of methods that are implemented with same name.

In Polymorphism we have 2 different types those are

- Compile Time Polymorphism (Called as Early Binding or Overloading or static binding)
- Run Time Polymorphism (Called as Late Binding or Overriding or dynamic binding)


Compile Time Polymorphism

Compile time polymorphism means we will declare methods with same name but different signatures because of this we will perform different tasks with same method name. This compile time polymorphism also called as early binding or method overloading.
Method Overloading or compile time polymorphism means same method names with different signatures (different parameters)

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 this run time polymorphism or method overriding we can override a method in base class by creating similar function in derived class this can be achieved by using inheritance principle and using “virtual &override” keywords.

In base class if we declare methods with virtual keyword then only we can override those methods in derived class using override keyword
Another way for run-time polymorphism in C# is that it allows you to create abstract classes that are used to provide partial class implementation of an interface. Implementation is completed when a derived class inherits from it. Abstract classes contain abstract methods, which are implemented by the derived class. The derived classes have more specialized functionality.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Practice
{
    class Program
    {
        static void Main(string[] args)
        {
     Polymorphism p = new Polymorphism();
            p.callmethodstaticpolymorphism();

            abstractclasspoly p1 = new abstractclasspoly();
            p1.calldynamicpolywithabstarct();


            virtualpoly p2 = new virtualpoly();
            p2.caldynamicpolywithvitualmethod();    
        }

    }
}
   
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Practice
{
    //Static polymorphism
    class Polymorphism
    {
        public void print(int i)
        {
            Console.WriteLine("int {0}", i);
        }
        public void print(double f)
        {
            Console.WriteLine("float {0}", f);
        }
        public void print(string s)
        {
            Console.WriteLine("string {0}", s);
        }

        public void callmethodstaticpolymorphism()
        {
            print(1);
            print(1.1);
            print("shoeb");

        }
    }
    //Dynamic polymorphism is done by ABSTRACT CLASS 
    class abstractclasspoly
    {
        abstract class shape
        {
            public abstract int area();
        }
        class recangle : shape
        {
            int h;
            int w;
            public recangle(int hi, int wi)
            {
                h = hi;
                w = wi;
            }
            public override int area()
            {
                return w * h;
            }
           
        }
        public void calldynamicpolywithabstarct()
        {
            recangle a = new recangle(12, 3);
            double c = a.area();
            Console.WriteLine(c);
        }
    }
//Dynamic polymorphism is done by VIRTUAL FUNCTIONS
    class virtualpoly
    {
        class baseshape
        {
            public int hieght;
            public int width;
            public  baseshape(int h, int w)
            {
                hieght = h;
                width = w;
            }
            public virtual int area()
            {
                return 0;
            }

        }
        class baserecangle:baseshape
        {
            public baserecangle(int h, int w):base (h,w)
            {

            }
            public override int area()
            {
                return hieght * width; 
            }
        }
        public void caldynamicpolywithvitualmethod()
        {
            baserecangle b = new baserecangle(10, 20);
            int a = b.area();
            Console.WriteLine(a);
        }
    }
}

please do correct if something wrong

No comments:

Post a Comment