Object Oriented Thinking : Operator Overloading and Polymorphism

Arvind Hari
Geek Culture
Published in
5 min readJun 6, 2021

--

Even the operators need to take up additional responsibilites

Overloaded (https://www.123rf.com/)

First of all, welcome to the last sector of the Introduction to Python Programming for Non-tech professionals’ series. I am excited to write this article and I am guessing you too are excited to read this. We have come quite a long way. The best part is, we get to end this series with a relatively easier and common concept.

All the codes snippets provided in this code and the outputs are stored in my Github repository.

Chuck the fancy terms, let us first try to contemplate on what overloading means?

Some entity is overloaded when it is asked to perform more than it is supposed to. The entity has been assigned to perform some actions, which are pre-defined but due to certain additional requirements, some additioanl actions are falling onto its head. Just like a responsible and accountable professional like you at times get over loaded with work as and when new responsibilities are picked up.

Well, the same things can happen to operators also. As per mathematics, the plus sign (addition operator), ‘+’, is supposed to add two numbers. But what if, we define a certain method implementation within a class that forces the plus sign to multiply two inputs. Sounds weird, right?? But here I am just trying to give a feel of what operator over loading is. We will look at the practical aspects soon enough.

What about Polymorphism then?

Yet another very common term which just refers to the art of capability of some entity to take up various forms. Well, let me consider myself as an entity, I am a fluid system analyst when working, a lazy human on weekends, a student trying to communicate Python while writing this article etc., get it ?? We ourselves display the concept of polymorphism on a daily basis.

Similarly, in programming, we can have the same objet perform differently at different instances. For example, the same method defined within a class can produce different return values for different arguments, sounds obvious, but this is just polymorphism being displayed. Another example, let us go back to the plus sign, the addition operator (+), as we discussed above, ignore the operator overloading part, just think about the plus sign we all know. It can performs arithmetical addition for numerical inputs, string concatenation for string inputs, list appending for list inputs, fascinating, isn’t it ??

Exactly, that is how Python implements Polymorphism.

Yes, I can guess the thought itching your mind, we could simply consider, OPERATOR OVERLOADING is just a form of POLYMORPHISM where we specify what the operator should do. We will talk more about this soon.

Let us jump into OPERATOR overloading then.

Operator Overloading

First things first

To exploit the feature of operator overloading, we need to use the in-built method implementation of the particular operator. We just perform a METHOD OVERRIDING here. As we are aware, all the user defined classes, all the methods defined inside it, whatever be it, all become a part of THE ‘main’ class by default.

In simpler terms, all the user defined classes become subclass of the master class ‘main’.

Let me just show a sample of the same.

Understanding __main__(), for Output, refer here

Did you notice the output, how the ‘dummy’ class has been represented. It has been respresented as a subclass of the inbuilt class main.

Well, why are we talking about this operator overloading ??

The default functionalities of all the operators are defined within this ‘main’ class. When we redefine the implementation of any of the OPERATOR using a method inside one of the user defined sub classes, we are just OVER RIDING its implementaion locally within this method.

It is slightly tricker to understand the implementation though. The updated operator implementation can be utilised by performing the desired operations between two class objects.

Let us implement it now for the addition operator (+)

Addition Operator Overloading Definition

‘ I completely accept, many of you might be lost right now, behold, it will make sense soon enough’

In case, the add method was not re-implemented, a simple (+) between the Object1 and Object2 would result in an TypeError. This is absolutely understandable. We ar trying to two instances of some user defined class. The addition operaion does not have a role here. On performing such additions, the interpreter will intimate us that we are using the + operator with unsupported operands.

But here, we are over riding the pre-defined definition. We are now telling, if two objects of this class are added, please implement whatever the return statement of the add() within this class returns.

Always remember, this can be performed only for two objects of the same class which has the in-built function modified. The number of objects involved in this operation depends on the operation, arithmetic operations like add, divide, multiply etc. are binary opeations (with 2 operands), hence two objects. The same idea can be extended to comparison, assingment and even unary operators.

Addition Operator Overloading Implementation

Quick reference for a list of available in-built implementations of various operators

Polymorphism

Well, there is nothing much to discuss further here. But let me just show one simple method implementation that takes two arguments and retunrs the result of addition operation.

Polymorphism Implementation

Congratulations

We have made it this far.

In case, you have been following the complete series of Introductory Python for Non-Tech engineers, I would say, we have captured all the basics of Python standard I/O to various data types to loops to functions to file handling or directory handling to logging to exception handling and finally basics of OOPs concepts.

The complete repo @ https://github.com/arvindhhp/PyPro_ahhp

The basic idea behind me presenting this sequence is to enable core engineers like MechEs, CivilEs and other professions irrespective of domain who have less to perform basic programming so that the power of Data can exploited through Machine Learning, complex data visualizations to enhance the existing capabilities.

With the understanding of the Python programming concepts covered in this series, we can consider ourselves to be eligible to go ahead and learn the art of exploiting data.

--

--