Object Oriented Programming


Python polymorphism


In programming, polymorphism refers to methods/functions/operators with the same name that can be executed on many objects or classes. "Polymorphism" means many forms.


Function Polymorphism

An example of polymorphism is len(). len() can be used with multiple data types like string, list, tuple, dictionary.


Class Polymorphism

Polymorphism is used in Class methods, where we can have multiple classes with the same method name.

Example

Let there be three classes class1, class2, class3.

Polymorphism example

number method is an example of polymorphism.

Polymorphism example

Output

first

second!

third!


Inheritance Class Polymorphism

Polymorphism works in inheritance too. When the parent class has a method and child class has a method of the same name.

Example

Let's look at the previous example

Polymorphism inheritance example Polymorphism inheritance example

Output

first

first

second!

third!