Does Python support polymorphism?


Yes, Python supports 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

  • Overloading
  • 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()

Output

The fish's skeleton is made of cartilage.
The clownfish's skeleton is made of bone.

Updated on: 12-Aug-2022

859 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements