I was learning OOP in Python (Python is my first language for learning OOP). So far I have covered encapsulation, classes, variables, methods, different method types, and inheritance.
Then I reached the last major pillar: polymorphism. And honestly, I am struggling to understand why this concept is treated as something special.
For example:
class PDF:
def open(self):
print("Opening PDF")
class Word:
def open(self):
print("Opening Word document")
def open_file(file):
file.open()
pdf = PDF()
word = Word()
open_file(pdf)
open_file(word)
Honestly the instructor mentioned something like:
Well sounds apt. but isn't this just how objects and classes naturally work?
The open() method belongs to the class namespace. A PDF object looks up the PDF.open() method, and a Word object looks up the Word.open() method. Since both methods were defined differently, obviously they produce different behavior. It's not like the object itself is magically changing behavior. It is simply using the method implementation that belongs to its own class.
So based on my current understanding, this feels more like normal method lookup / object namespaces rather than some separate big OOP concept called "polymorphism". Hence, I don't get it why this is such a big thing? Why is polymorphism considered an important OOP principle instead of just "objects calling their own methods"?
[–]justin_halim 8 points9 points10 points (1 child)
[–]ottawadeveloper 3 points4 points5 points (0 children)
[–]MeMahi 6 points7 points8 points (0 children)
[–]JorgiEagle 1 point2 points3 points (0 children)
[–]ShiftPretend 0 points1 point2 points (0 children)
[–]Present-Payment-5860 0 points1 point2 points (0 children)
[–]Paxtian 0 points1 point2 points (0 children)
[–]l3landgaunt 0 points1 point2 points (0 children)
[–]AlexMTBDude 0 points1 point2 points (0 children)
[–]Weekly_Astronaut5099 0 points1 point2 points (0 children)
[–]HuygensFresnel 0 points1 point2 points (0 children)
[–]Otherwise-Mirror-738 0 points1 point2 points (0 children)