Function overloading is not polymorphism.

Introduction

Polymorphism in programing language is to implement an essential non-functional requirement called flexibility. Client should not get affected by any behavioral changes in S/W done by any developer.

Many book and articles says that function overloading is a type of polymorphism. Some report says that function overloading is a compile-time polymorphism. If you go by the definition of polymorphism, it says “many forms under one name.”

Now see function overloading closely. The name of the function is the same, but the signature of functions are different. Can we call a function by knowing just its name? The answer is no. We have to have a knowledge of its full significance. For example –
             
                       1.       Int Add(int firstNumber, int secondNumber);
                       2.       Float Add(float firstNumber, float secondNumber);
                       3.       Double Add(double firstNumber, double secondNumber);
                         And so on ………………





According to the design principle. Whatever I do inside my black box (system X) client should not get affected by it. But here if I add one more function (or behavior) which will add two strings now –
                        String Add(string firstString, string secondString);

Can the client call it without knowing its signature? No. I have to inform the client about my new behavior changes. With its unique signature or name. 

Each signature is different. Which is not a polymorphism. Here treat signature as a name (or new behavior of system X) because, without signature, you cannot call it. Just the name of a function is not going to help you.

Now, what is the difference between having 4 functions of different names and having the same name add; With four different signature? Function wise my code is going to work in the same way, isn’t it?

Actually, polymorphism is something like below –


Under same function name or signature (for example – SomeFunction()) it should behave differently according to situation or context.

What is functions overloading?
Remember control units in MVC model? Control units are a very classic example of function overloading. Let’s understand it through an example. Calculator.



Does it make any changes in execution if I have different name of function add, in control unit? Like mentioned below -
                       1.       Int Add1(int firstNumber, int secondNumber);
                       2.       Float Add2(float firstNumber, float secondNumber);
                       3.       Double Add3(double firstNumber, double secondNumber);

Instead of having the same name –

                      1.       Int Add(int firstNumber, int secondNumber);
                      2.       Float Add(float firstNumber, float secondNumber);
                      3.       Double Add(double firstNumber, double secondNumber);

Will I achieve something in execution point of view? No, my code execute and work in the same manner for both cases. Then what is function overloading?

Function overloading is nothing but a nomenclature. This shows that these many functions have the same name. The system will work in the same way with the same or different name. But it is a tool to group same functions under one function name. That is why name mangling has introduced in C++, but it was not there in C language. Just for same grouping behaviors. So that we can increase the readability of code and reduce maintenance cost. By seeing only, any new coder will understand. That all functions with the same name are somewhat similar in execution.

How to achieve polymorphism?

Now we are talking about the real problem, different execution of functions in the same name (signature). Which looks like below –




But technically, it is not possible to have the same function name in same address space. Why? Because the compiler will get confused which one to call. Then what is the solution? Solution has the same name with different address space something like below –


Now the answer sounds like function overriding. Remember, in function overriding, we have different implementation, but under the same function name (same signature). That’s called polymorphic implementation.

Now, if you add new behavior, no need to tell the client about the unique signature. As it will have the same old name. Now, it depends upon the library developer, on how he implements internal code without disturbing the outer world/client. Coder can change/Add/Remove logic/behavior anytime under the same signature. 

Thanks for reading it. To learn more about design patterns and basic design principles, please see my web page.

Comments

Popular posts from this blog

Non-virtual interface idiom (NVI)

Architectural patterns => Mud to structure => layers.

Architectural style -> Adoptable system -> Reflection.