Python Object-Oriented Programming: Polymorphism
The word Polymorphism is made by two Greek words, Poly meaning many and Morph meaning forms. In programming, it refers to the same object exhibiting different forms and behaviors.
For example, there is a parent class: Shape, and there are four child classes extended from the parent class: Rectangle, Circle, Polygon, and Diamond. Suppose you need methods to calculate the area of each specific shape. You could define separate methods in each class, getSquareArea(), getCircleArea(). But this makes it difficult to remember each method’s name. Is it possible to just define one method called getArea()?
The base class declares a function getArea() without providing an implementation. Each derived class inherits the function declaration and provides its own implementation.
# Parent Class
self.sides = 0 def getArea(self):
def __init__(self, width=0, height=0):
self.width = width
self.height = height
self.sides = 1 def getArea(self):
return (self.width * self.height)class Circle(Shape):
def __init__(self, radius=0):
self.radius = radius def getArea(self):
return (self.radius * self.radius * 3.14)
These derived classes inherit the getArea() method, and provide a shape-specific implementation, which calculates its area. This is Polymorphism; having specialized implementations of the same methods for each class. In this example, the Rectangle and Circle classes were overriding the getArea() method from the Shape class.
It is one of the most useful concepts in Python Object-Oriented Programming. We can implement polymorphism without using inheritance by using duck typing.
Duck typing extends the concept of dynamic typing in Python. Due to the dynamic nature of Python, duck typing allows the user to use any object that provides the required behavior without the constraint that it has to be a subclass.
x = 5 # type of x is an integer
print (type(x))x = "Medium" # type x is now string
Implementing Duck Typing
print("Woof Woof")class Cat:
print("Meow meow")class AnimalSound:
def Sound(self, animal):
We can use any property or method of animal in the AnimalSound class as long as it is declared in that class.
In layman terms, since both the animals, dog and cats, can speak like animals, they both are animals. This is how we have achieved polymorphism without inheritance.
Abstract Base Classes
Duck typing is useful as it simplifies the code and the user can implement the functions without worrying about the data type.
Abstract Base Classes define a set of methods and properties that a class must implement in order to be considered a duck-type instance of that class.
To define an abstract base class, we use the abc module. The abstract base class is inherited from the built-in ABC class. We have to use the decorator @abstractmethod above the method that we want to declare as an abstract method.
from abc import ABC, abstractmethodclass ParentClass(ABC):