Polymorphism and its best practises

In this section of OOAD we will discuss the all important topic of Polymorphism. We have already discussed this while we were discussing Association and Encapsulation.

We have discussed that one behaviour can have multiple implementations.

As per the definition of Polymorphism, it says that, "keeping the behaviour constant, switching the implementation of behaviour at run time is called Polymorphism".

If you have to implement Polymorphism within your programming languages, how would you do it within your system?

Whenever I ask this question, the two most common answers that I get are "Overloading" and "Overriding". Sometimes people also talk about "Static polymorphism" and "Dynamic Polymorphism".

Let me again give you the definition of Polymorphism. Think about it carefully and then decide whether "Overloading" is a way of implementing polymorphism within your system.

The definition of Polymorphism says "keeping the behaviour constant, switching the implementation of behaviour at run time is polymorphism".

Do you still think that "Overloading" is a way of implementing Polymorphism within your system?

I hope you will agree that in Overloading, you are re-using the "method name" but the "method signatures" are different.

Technically, Overloading is a simple "method call" with the "method name" reused.

I hope you will agree that in overloading, the method signature changes.


But that is not the definition of Polymorphism.

Let's break the definition and analyse each small part. The definition says that "keeping the behaviour constant". What do you understand by "keeping the behaviour constant"?

Behaviour syntactically is "Method-Signature".

So I can rephrase the definition by saying that "keeping the method signature constant, switching the implementation of that corresponding method signature at run time is called Polymorphism".

As per the definition we cannot say that Overloading is a way of implementing Polymorphism.

Whenever I say this, most of the participants are not happy with it. They tell me how can I say something like this? Many times people ask me as to why do I add the clause of "run time" at the end of it. It is because of the "run time" that the definition goes for a toss.

The answer to the above problems will be derived from the following question.

Why is Polymorphism needed within your system?

I am questioning as to why was the definition phrased as "keeping the behaviour constant switching the implementation of behaviour at run time is called Polymorphism"?

Why would someone want to change the behaviour at run time?

I am asking why Polymorphism? Why would someone want the system to behave in a way that the signature should remain the same but still the implementation should vary? Why would anyone want to change the implementation of behaviour at run time?

Don't you think Polymorphism is a way of incorporating "Flexibility" within your system?

Think about this. Why would someone want to change the implementation of the behaviour?

According to me, changing the behaviour simply means incorporating flexibility.

When we were discussing MVC, we had discussed that flexibility is all about managing change.

So the first thing we need to understand is why are systems polymorphic? And why are the systems supposed to be Polymorphic?

Systems are supposed to be Polymorphic so that they can take care of Flexibility. Polymorphism is needed within systems so that they can be flexible.

Now can you tell me, can "overloading" be a way of incorporating flexibility within your system?

When we discussed Flexibility, Classification of Flexibility, Structural and behavioural flexibility, we had discussed a few rues wrt flexibility.

What was the first rule? What was the first rule for a highly flexible system?

The first rule for flexibility says that any kind of internal change happening within your system, should not be visible outside. It should not be visible to the user of the corresponding system.

Keeping this rule in mind, now say can overloading be a way of incorporating flexibility within your system?

Look at the figure on the next page.

Figure

In this figure I have shown that if you were trying to incorporate Flexibility using overloading, how it would look like.

In this figure, I have a system which is offering 4 different signatures, with the method name 'B' being reused. But for every method there is a different signature and for every signature there is a different implementation.

As you know in this case all the signatures are always available. The system always exposes the signatures. And as you can see, the user is aware of all the signatures which are offered by the system.

Think about it, tomorrow if you want to add one more implementation to it, a new signature will come in. The moment a new signature comes in; don't you think the user should be aware of it?

So please understand that if you are trying to incorporate flexibility by using overloading as a way, it cannot be the right way to do the same. This is because every time you make a change, the user is aware of it. And that is against the principle of flexibility.

So please remember that every time you use Overloading as a way to incorporate flexibility, with every change happening, the user is aware of it and that's the reason overloading cannot be used as a way of incorporating flexibility in a system.

Now think about it. Why was the definition of Polymorphism phrased in a way that says the "signature" should remain constant but still the implementation should vary?

Please have a look at the figure below.

Figure 2

Here I have shown you a case where we have talked about a single signature, where there is a method B which typically takes 2 string as an argument. You can see that there are different implementations within this corresponding system for the same signature.

This is just to show you as to why was the definition framed that the "signature should be constant". Only when the signature is constant, only when the signature doesn't change, the user will not be aware of the different implementations which are happening within your system.

I have just shown you graphically as to why this definition was framed that keeping the behaviour constant; switching the implementation of behaviour at run time is polymorphism.

I am not saying that overloading is not needed within your system. Overloading is needed within your systems but not for Flexibility.

Then where exactly is Overloading needed?

We have discussed Control entities. Like reservation control entity wherein there is a reservation control entity wherein in the reserve methods can take different arguments. For such a kind of thing you would definitely need overloading. But saying that using Overloading as a way of incorporating flexibility, there are some technical flaws with it.

I have also shown you as to why the definition of Polymorphism says that the signature is should be constant because if the signature is not constant, then the user would be aware of it and again you cannot say that it is the right way of incorporating flexibility.

So now tell me how will you incorporate or implement flexibility within your system? Using programming languages

Look into the figure again. Ideally what should happen is keeping the signature constant, you should be able to change the implementation of the behaviour at run time.

But let me tell you. This is the second limitation of your programming languages.


 
Hemant Jha
Founder - VPlanSolutions
Researcher, Trainer

www.VPlanSolutions.co.in