Polymorphism And Method Overloading Are NOT The Same Thing!!


The first real question I ask when interviewing new developers is almost always some form of the following: "What is polymorphism and what are some ways of implementing it in C# (or whatever programming language). The most common answer I get is that polymorphism is the same as method overloading. Although you can make a case that method overloading is an example of polymorphic behavior, it is absolutely false that they are one and the same thing. Those developers who really believe that polymorphism and method overloading are one and the same are doing themselves a terrible injustice. Polymorphism means "many shapes" or "many forms." The real power of polymorphism is how to execute, or communicate with, a method is distinctly separate than how that works. Polymorphism  is the way we encapsulate/abstract behaviors on object-oriented programming languages. This allows us to swap out behaviors, not just over the life of a system, but even at run-time.
Here is a simple example of what I mean. Take the following code:
internal class Program
{
    private static void Main(string[] args)
    {
        var program = new Program();
        var phrase = "Hello World!!";
        program.Print(phrase);
        program.Print((object)phrase);
        Console.ReadLine();
    }
    public void Print(object objectToPrint)
    {
        Console.WriteLine("object:{0}", objectToPrint);
    }
    public void Print(string objectToPrint)
    {
        Console.WriteLine("String:{0}", objectToPrint);
    }
}
The output of the code is this:
String:Hello World!!
object:Hello World!!
The reason for this is that method overloading is only "polymorphic" at design time. Once the code is compiled it is all statically bound to the specific implementation based on the method signature.
Now lets look at an example of run time polymorphism:
 internal class Program
{
    private static void Main(string[] args)
    {
        var program = new Program();
        var animals = program.GetAnimals();
        Array.ForEach(animals, program.Print);
        Console.ReadLine();
    }
    private IAnimal[] GetAnimals()
    {
        return new IAnimal[] { new Dog(), new Cat(), new Human() };
    }

    public void Print(IAnimal objectToPrint)
    {
        Console.WriteLine("{0}:{1}", objectToPrint.GetType().Name, objectToPrint.Speak());
    }

}
public interface IAnimal
{
    string Speak();
}

public class Dog : IAnimal
{
    public string Speak()
    {
        return "Bark Bark";
    }
}
public class Cat : IAnimal
{
    public string Speak()
    {
        return "Meow";
    }
}
public class Human : IAnimal
{
    public string Speak()
    {
        return "Hello World";
    }
}
and the output:
Dog:Bark Bark
Cat:Meow
Human:Hello World
4 comments

4 comments :

  1. While during my college days while I was learning C++. I was told of static polymorphic and dynamic polymorphic that OOP offers. Static Polymorphic refers to method overloading and Dynamic Refers to Method Overriding. These days, many new developers stick to method overloading. They need to revisit the classrooms to understand better on these concepts. Another relevant concept that I learn is Static Binding and Dynamic Binding. I hope the young developers are aware of it. Nice Article.

    ReplyDelete
    Replies
    1. So true. And the fact that they "stick to method overloading" or "static polymorphism" makes it virtually impossible to really understand Design Patterns such as those described by the Gang of Four (http://www.amazon.com/Design-Patterns-Elements-Reusable-Object-Oriented/dp/0201633612) that depend on a more "dynamic" type of polymorphism.

      Delete
  2. The confusion seems to be over different types of polymorphism. Method overloading is an example of ad-hoc overloading. While your rant is more concerned with sub-type polymorphism. You may also want to check out parametric polymorphism. (i.e. generics). Strictly speaking method overloading is a way of expressing polymorphism. It's simply not the whole story.

    ReplyDelete
  3. I meant to say ad-hoc polymorphism above not ad-hoc overloading.

    ReplyDelete