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

kungtotte profile image
Thomas Landin

In a very general sense, all code is abstraction :)

What people mean when they talk about Abstraction is really about turning some code into a more generalized concept. Usually this goes hand in hand with encapsulation, but the two aren't the same and they can be used separately.

If we think about what the word Abstract means, it means "apart from concrete existence". Which is a fancy way of saying it's about describing a real thing in your own words, to make thinking about that real thing easier.

If you're writing some code and you feel like it gets too much to keep track of you might think to yourself "I will make a function to gather up these lines". So you turn your four or five lines of code into a function called user_login() and call it instead of writing those five lines out. user_login() is now an abstraction, instead of the concrete (the five lines you need to execute to login a user) you wrote the abstract concept user_login().

You made up your own word to describe the real thing.

It's not about hiding implementation (anyone can go look at user_login() after all), but about simplifying and generalizing the code in front of you.