Does Python support polymorphism?

Object Oriented ProgrammingProgrammingPython

Yes,Python support polymorphism.

The word polymorphism means having many forms.

polymorphism is an important feature of class definition in Python that is utilised when you have commonly named methods across classes or sub classes.

Polymorphism can be carried out through inheritance, with sub classes making use of base class methods or overriding them.

There are two types of polymorphism

• Overriding

Overloading: Overloading occurs when two or more methods in one class have the same method name but different parameters.

Overriding: Overriding means having two methods with the same method name and parameters (i.e., method signature). One of the methods is in the parent class and the other is in the child class.

Example

class Fish():
def swim(self):
print("The Fish is swimming.")

def swim_backwards(self):
print("The Fish can swim backwards, but can sink backwards.")

def skeleton(self):
print("The fish's skeleton is made of cartilage.")

class Clownfish():
def swim(self):
print("The clownfish is swimming.")

def swim_backwards(self):
print("The clownfish can swim backwards.")

def skeleton(self):
print("The clownfish's skeleton is made of bone.")

a = Fish()
a.skeleton()
b = Clownfish()
b.skeleton()



When we run the program with the python polymorphism.py command, we will get an expected output −

Output

The fish's skeleton is made of cartilage.
The clownfish's skeleton is made of bone.
Updated on 29-Jun-2020 11:15:06