Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Python by (19.9k points)
edited by

I have been reading about oop and i came across polymorphism. I understood it theoretically but I don't know how to apply it in real life? Can someone give me a clarifying example.

1 Answer

0 votes
by (25.1k points)
edited by

Polymorphism is a greek word in which poly stands for many and morphism stands for forms. It gives you the ability to define an interface that can be used to preform different tasks. In simpler terms, it means that if a class inherits from another class, it doesn’t have to inherit everything about parent class, it can do some of the things that  the parent class does differently. It is usually used in conjunction with inheritance. Python provides you with ability to overload standard operators which makes them behave differently based on the operands such as the addition operator being used for both numeric addition and string concatenation.

Here's an example:

x, y = 1, 2

x + y # Output: 3

x, y = "1", "2"

x + y # Output: "12"

Take a look at this python course to get a deeper understanding on python.

Browse Categories

...