DEV Community

Discussion on: Explain Encapsulation and Polymorphism Like I'm Five

Collapse
 
mdabek profile image
Marek Dabek

Encapsulation and polymorphism are general object oriented programming concepts, together with abstraction and inheritance, they form four pillars of OOP.

The encapsulation is hiding the details of "how the thing is working" and shows only single/few buttons to press to "make the thing working". (Stepping out of the ELI5 mode: These may be implemented via private/public or via interface and interface implementation, public API calls, etc.)

Polymorphism allows to modify class behavior based on the data type you provide. If you imagine an oven, it allows to bake a cake, roast veggies, roast nuts, grill tofu. You use it a bit different depending on what type of food you input to it. The same concept exists in the OOP world:

void PlaySound (AmbientSound snd)
{
dimm_the_lights();
set_volume(LOW);
play(snd)
}

void PlaySound (AlertSound snd)
{
turn_on_alert_lights();
set_volume(MAX);
play(snd);
}

Further reading: beginnersbook.com/2013/03/oops-in-...