Polymorphism in software development

The word is ‘Polymorphism’. Greek origin. Meaning is different functions under same interface. For example, steering of a car. It may be manual steering or power steering. Their internal function is different. But for a driver steering interface is same. So that one driver can drive with two types of steering. There is no need to learn two different driving skills for two types of steering. So by polymorphism we can use our knowledge in different closely related implementations. Because for us the interface is same. We are familiar with that interface.

Polymorphism in software development

Just like our practical example polymorphism is applicable in our software development. Suppose we are developing software for client. After some times we also upgrade that software and add some new functionality in to that, as well as change some existing functionality. If we maintain the user interface as unchanged or familiar when we are upgrading the functionality, then it is better to use the new functionality for our client without any further training. Because for the client user interface is same, only under the same user interface the functionality has changed. This is polymorphism.

Polymorphism in Microsoft .NET Framework

In this post I will mention what are types of polymorphism and how we can implement polymorphism in our daily software development with Microsoft .NET Framework and C# programming language.

What are the types of polymorphism?

There are two types of polymorphism in .NET.

  • Static or compilation time polymorphism. (Early binding)
  • Dynamic or runtime polymorphism. (Late binding)

Static polymorphism

Static polymorphism means the polymorphism which is performed in compilation time. We can implement static polymorphism in three ways.

  • Method overloading.
  • Operator overloading.
  • Indexer overloading.

Method overloading

When we declare more than two methods in a class having the same name but different parameter list, then it is called method overloading. The methods are called overloaded methods. When we call the method, depending on the parameter list compiler makes a decision that which version of the overloaded method will be called.

public int Add(int x, int y)
{
    return (x + y);
}

public double Add(double x, double y)
{
    return (x + y);
}

Operator overloading

We use operator to perform some operation on operands. There are many types of operator in programming language. Now the operands are primitive types. If we want to use the objects of our user defined class to be as operands of a particular operator, we have to overloads that particular operator in our user defined class.

class MyClass
{
    private int x;
    private int y;

    public MyClass()
    {
    }

    public MyClass(int x, int y)
    {
        this.x = x;
        this.y = y;
    }

    public static MyClass operator +(MyClass obj1, MyClass obj2)
    {
        MyClass obj = new MyClass()
        {
            x = obj1.x + obj2.x,
            y = obj1.y + obj2.y
        };
        return obj;
    }

    public override string ToString()
    {
        return string.Format("X = {0}\nY = {1}", x, y);
    }
}

Indexer overloading

It is same as method overloading. Compiler decides which version of the indexer will be called based on the parameter type.

public string this[int index]
{
    get{ }
    set{ }
}

public string this[string position]
{
    get{ }
    set{ }
}

Dynamic polymorphism

Dynamic polymorphism means the polymorphism which is done not in compilation time but in runtime. There are four ways through which we can implement runtime polymorphism.

  • Method overriding.
  • Property overriding.
  • Polymorphism through Delegate.
  • Polymorphism through Interface.
  • Method and property overriding

Base class having general functionality, derive class means specific functionality. So base class having the methods and property which implement general functionality for all derive classes (some times they become abstract). Derive class may override that general functionality of base class to a more specific functionality. Then this is called overriding. Base class method or property is declared as ‘virtual’ and derive class method or property is declared as ‘override’. At runtime it is decided that what version (base or derive) will be called depending on the particular object.

class Base
{
    public virtual string SomeProperty
    {
        get
        {
            // get base implimentation.
        }
        set
        {
            // set base implimentation.
        }
    }

    public virtual string SomeMethod()
    {
        // Base implimentation.
    }
}

class Derive : Base
{
    public override string SomeProperty
    {
        get
        {
            // get derive implimentation.
        }
        set
        {
            // set derive implimentation.
        }
    }

    public override string SomeMethod()
    {
        // Derive implimentation.
    }
}

Notice its polymorphic nature.

Base b1 = new Base();
b1.SomeMethod(); // Call base version.

Base b2 = new Derive();
b2.SomeMethod(); // Call derive version.

Polymorphism through Delegate

Delegate means managed function pointer in .NET Framework. We can assign method reference in to a delegate, if the signature of the delegate and method are same. Through delegate we can create a polymorphic type of data structure.

delegate string GetMessage(string name);

class Welcome
{
    public string GetWelcomeMessage(string name)
    {
        return string.Format("Welcome {0}", name);<br />
    }
}

class Bye
{
    public string GetByeMessage(string name)
    {
        return string.Format("Bye {0}", name);
    }
}

class DelegateDemo
{
    static void Main(string[] args)
    {
        GetMessage g;

        Welcome w = new Welcome();
        g = w.GetWelcomeMessage;
        System.Console.WriteLine(g("Rahul")); // Out put: Welcome Rahul

        Bye b = new Bye();
        g = b.GetByeMessage;
        System.Console.WriteLine(g("Rahul")); // Out put: Bye Rahul
    }
}

Polymorphism through Interface

Interface is a service contract between service provider and service consumer, irrespective of how the service will be provided. Many different classes can implement a single interface with different implementation.

interface IGetMessage
{
    string Get(string name);
}

class Welcome : IGetMessage
{
    public string Get(string name)
    {
        return string.Format("Welcome {0}", name);
    }
}

class Bye : IGetMessage
{
    public string Get(string name)
    {
        return string.Format("Bye {0}", name);
    }
}

Notice its polymorphic nature.

IGetMessage message;

message = new Welcome();
System.Console.WriteLine(message.Get("Rahul")); // Out put: Welcome Rahul

message = new Bye();
System.Console.WriteLine(message.Get("Rahul")); // Out put: Bye Rahul

Here I have tried to show how polymorphism is implemented in software development through some small and easy to understand examples. Hope this will help you a lot in your development time.

You can also download this code from MSDN code sample.
Download from MSDN code sample

Thank you.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s