Myth about Polymorphism

Myth about Polymorphism

If you believe that 'Compile Time' or 'Static' polymorphism exists, then this is a must-read article for you.

Polymorphism is a Greek word that means "many-shaped" or “many-forms”

 Many people have categorised polymorphism as compile time vs runtime OR static vs dynamic. It is very commonly believed that overloading is compile time or static polymorphism and overriding is runtime or dynamic polymorphism. But let me tell you that polymorphism is polymorphism, and it can be only seen at the runtime. Overloading is accidentally co-related with polymorphism whereas overloading is simply a language feature and has nothing to do with polymorphism

 Let’s have a look at below example:

No alt text provided for this image
No alt text provided for this image

Since Add() shows two different output (30 & 50) for two different calls, will you say that Add() is polymorphic? Of course NOT and no one will not call this as polymorphic behaviour as we have changed the input (values passed as parameters) while giving calls to Add(). And if we change the input while calling a method, output is bound to change. Hence, we do not see any polymorphism in above code snippet.

 Now let’s see another scenario of method overloading.

No alt text provided for this image
No alt text provided for this image

Now look at both the method calls carefully, have we changed the input (values passed as parameters) while giving second call to Add()? Yes, we did change the input. And since input itself is changed, output is bound to change in this scenario as well. Hence, this code snippet doesn’t have polymorphism in it. Method overloading is a language feature that allows more than one method to share a common name but have different patterns of their parameters. When such overloaded methods are called, compiler matches the patterns of the parameters and decides which method from the overloaded methods to call.

 Now let’s talk about overriding.

No alt text provided for this image
No alt text provided for this image

In above code snippet, we called Add() without changing any input (values passed as parameters) but we can see two different outputs (“Addition using BasicCalc: 30” & “Addition using AdvCalc: 30). Method from which class is to be called is decided by the runtime engine based on the object to which the reference variable points to. And this is the polymorphism in true sense. Polymorphism is a special ability given to a reference variable so that it can access selected member(s) from the derived class, which is impossible ordinarily. In above example, base class reference ‘calc’ points to derived class ‘AdvCalc’ by leveraging polymorphism’s virtual dispatch mechanism, ‘calc’ can call a method from its derived class.

 Summary:

 In simple words, we need to unlearn compile time or static polymorphism. Overloading is not polymorphism. Polymorphism is possible only by using overriding capabilities provided by language frameworks.

Reference:


To view or add a comment, sign in

Others also viewed

Explore content categories