Similar to inheritance, polymorphism is an important paradigm in object-oriented programming. Polymorphism in object-oriented programming refers to the ability of different classes to be treated as instances of the same class through a common interface. Let’s start with a very simple example:
# This is an example of polymorphism in Python
class MathClass:
def __init__(self,x,y):
self.x=x
self.y=y
def multiply_function(self):
result=self.x*self.y
return result
class MathClass2:
def __init__(self,x,y,z):
self.x=x
self.y=y
self.z=z
def multiply_function(self):
result=self.x*self.y*self.z
return result
firstObject=MathClass(5,10) # creating an object
secondObject=MathClass2(4,2,10) # creating an object
print(firstObject.Multiply_function()) # Accessing a method
print(secondObject.Multiply_function()) # Accessing a method
Output:
50
80
Here, we have two different classes, MathClass
and MathClass2
. Both classes have the same method name multiply_function()
, but one takes two parameters, and the other takes three. We see two different forms of the same function.
Operator Overloading
We are already familiar with various operators in Python. What does an operator do? In simple terms, it performs an operation on one or more operands. For example, the +
operator adds two operands, and the -
operator subtracts one operand from another. But what if we want to add or subtract objects? Directly, it is not possible. We need to overload the operator, which means we need to write additional code to define the behavior for objects. This is called operator overloading. Here’s an example:
# This is an example of an operator overloading in Python
class TestClass:
def __init__(self,x):
self.x=x
def __add__(self,secondclass):
y=self.x+secondclass.x
return y
firstObject=TestClass(5) # creating an object
secondObject=TestClass(10) # creating an object
print(firstObject+secondObject) # + Operator Overloading
Output:
15
Here, the result is 15, meaning the +
operator can now add objects. In line 12, the +
operator is overloaded. The __add__
function is used to overload the +
operator. Similarly, the __sub__
and __mul__
functions are used to overload the -
and *
operators, respectively.
Function Overloading
Function overloading refers to defining multiple functions with the same name but different arguments. Unlike C++, function overloading in Python is not straightforward. Here’s an example:
# This is an example of an function overloading in Python
class TestClass:
def greetings(self, surname=None):
if surname!=None:
print("Welcome "+surname)
else:
print("Welcome!")
testObject=TestClass() # creating an object
testObject.greetings() # Function Overloading
testObject.greetings("Shahinur") # Function Overloading
Result:
Welcome!
Welcome Shahinur
As mentioned, Python doesn’t support function overloading directly. In the function definition in line 3, a default value of None
is used to achieve the same behavior. If no value is passed, the default value None
is used, but if a value is provided, it will be used.
In lines 11 and 12, the same function is called with different parameters, demonstrating function overloading.
Function Overriding
Function overriding is primarily associated with inheritance. It occurs when a child class provides a specific implementation of a method already defined in its parent class. Here’s an example:
# This is an example of an function overriding in Python
class Class1:
def __init__(self,x,y):
self.x=x
self.y=y
def doing_something(self):
z=self.x+self.y
return z
class Class2(Class1):
def __init__(self,x,y):
self.x=x
self.y=y
def doing_something(self):
z=self.x*self.y
return z
firstObject=Class1(5,10) # creating an object
secondObject=Class2(5,10) # creating an object
print(firstObject.doing_something) # Function overriding
print(secondObject.doing_something) # Function overriding
Result:
15
50
Here, the doing_something()
function is overridden. Although it exists in the base class, it is modified in the child class.