Topic : Polymorphism in C#: Static Polymorphism | Dynamic Polymorphism

What is Polymorphism:

Polymorphism is key to the power of Object-Oriented-Programming (OOP). it's so Important, the languages that don't support Polymorphism can't advertise themselves as OOP languages. Languages that support classes but not polymorphism are called Object-Based Languages.

Polymorphism comes from Greek word, meaning "Many Forms". It is the Quality that allows one interface to access a General class of Actions. A simple Example of Polymorphism is found in the Steering Wheel of an Automobile. The Steering wheel ( The Interface) is the same no matter what type of actual Steering Mechanism is used. That is, the steering wheel works the same whether our car has manual steering, power steering, or rack-and-pinion steering. Thus, turning the steering wheel left causes the car to go left no matter what type of steering is used. The benefit of the uniform interface is, of course, that once we know how to operate the steering wheel, we can drive any type of car.


image: Polymorphism

  • Polymorphism are of two types:

    » Static or Compile Time Polymorphism.
    » Dynamic or Runtime Polymorphism.



Static Polymorphism:

The Compiler is able to select the Appropriate Method for a Particular call at compile time itself. This is known as Compile Time Polymorphism, also known as early binding.




Method Overloading:

Whenever same Method name is existing Multiple time in the same class with different number of Parameter or different order of Parameters or different type of Parameter is known as Method Overloading.


  • Ways to Overload a Method:

    » By Changing Number of Arguments.
    » By Having Different Types of Arguments.


Let's have an simple example to know how we can use the concept of Polymorphism by overloading the Method.



/*Example: Polymorphism - Method Overloading : InfoBrother: */

using System;

namespace polymorphism
{
    class overload  
    {
        public int add(int x)  //Method:  1st:
        {
            return x;
        }

        public int add(int x, int y)  //Method Overload:  2nd:
        {
            return x + y;
        }

        public int add(int x, int y, int z)  //Method Overload again: 3rd:
        {
            return x + y + z;
        }
    }  


    class Program   //main class.
    {
        static void Main(string[] args)
        {
            overload obj = new overload();  //Object:
            Console.WriteLine(" X = {0}", obj.add(10)); //1st:
            Console.WriteLine(" X + Y = {0}", obj.add(10, 20)); //2nd:
            Console.WriteLine(" X + Y + Z = {0}", obj.add(10, 20, 30)); //3rd:
            Console.ReadKey();
        }
    }
}


The Above Example is really easy to understand the concept of Polymorphism in the sense of Method Overloading. In above example, we Overload the single Method add() two time with different Parameters. and Our compiler is really sharp to understand that which Method do we calling in our main() program, due the Number of Arguments.



Method Overloading Vs Method Overriding:

Method Overloading and Overriding are two different Things: If we Inherit a Class into the Derived Class and Provide a Definition for one of the Base class's Method again inside the Derived Class, then is known as Method Overriding. While in case of Method overloading, we overload the method in the same class.


We Will Discuss About Operator Overloading in our Operator Overloading Tutorials:


Dynamic Polymorphism:

Dynamic Or Runtime Polymorphism is Achieving when object Method Should invoked during Runtime instead of compile time. This type of Polymorphism is also known as Method overriding or late binding.

In this type of Polymorphism, we can override a Method in base class by creating similar Method in derived class. this can be achieved by using inheritance Principle and Using Virtual and Override Keywords.



In Base Class if we Declare Methods with Virtual Keyword then only we can override those Methods in Derived class Using Override.


Let's have an Example, where we will see how we can achieve Dynamic Polymorphism using the keywords Virtual and Override.



/*Example: Polymorphism - Dynamic Polymorphism : InfoBrother: */

using System;

namespace polymorphism
{
    class Shape   //base Class:
    {
        public int radius = 7;
        public virtual double Area()  //Virtual Method:
        {
            return 0;
        }   
    }

    class Circle : Shape   //Derived Class:
    {
        public override double Area()  //Overridden Method:
        {
            return 3.14 * radius * radius;
        }
    }

    class Sphere : Shape   //Derived Class:
    {
        public override double Area()  //Overridden Method:
        {
            return 4 * 3.14 * radius * radius;
        }
    }



    class Program   //main class.
    {
        static void Main(string[] args)
        {
            Circle obj1 = new Circle(); //Object of type circle:
            Sphere obj2 = new Sphere(); //Object of type Sphere.


            Console.WriteLine("The Area of Circle is: " + obj1.Area());
            Console.WriteLine("The Area of Sphere is: " + obj2.Area());

            Console.ReadKey();
        }
    }
}


















I Tried my Best to Provide you complete Information regarding this topic in very easy and conceptual way. but still if you have any Problem to understand this topic, or do you have any Questions, Feel Free to Ask Question. i'll do my best to Provide you what you need.

Sardar Omar.
InfoBrother





WRITE FOR INFOBROTHER

Advertising






Advertisement