DEV Community 👩‍💻👨‍💻

Discussion on: The PIE concept of OOP.

matthiasuwe profile image


I like the article and it goes down to basic principals, which are really important for OOP.

But I'm not going along with your examples. Let me show you for Polymorphism:

"A person can be happy at this moment and at the next moment he can be sad or angry. Here, a single person is functioning multiple emotions. This is how Polymorphism works! Relatable enough?"

What you are describing, is a state change within the same object - one person is reacting differently but not because of Polymorphism but because of emotions (attribute) of the object.

Run-time Polymorphism means, you have different objects and calls them all the same way. But they are reacting differently (overwritten methods).

Let's keep the example of people - think about a situation, there is an medical emergency and 10 people are standing around. You are asking each of them "Please help this person!" - 9 of them are responding with "I will call an ambulance". But one will start to re-animate the person?
Why? This is about Polymorphism. You have the same, narrowed view (narrowing cast) to these 10 people, but they are not just "people", they might be layers, construction workers or something different. But one out of the 10 is a doctor who has the method "helpInMedicalEmergency" function overwritten. This person is able to help immediately, the rest of the people have "standard" implementation (from parent class) for the same function.

The important point - the moment, you are asking, you just know each of them is "people" but while asking (runtime) the actual object decides about the most specific implementation which will be executed.

What do you think?

Kind regards,

enakshi_pal profile image
Enakshi Pal Author

Hey, I liked the way you elaborated the example. Quite understandable! Thanks!

romaincarrillo profile image

Hi Matthias,
Thank you for this exemple, it makes it way more clear to me.