DEV Community

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

Collapse
 
cjbrooks12 profile image
Casey Brooks

Encapsulation and Polymorphism are often talked about together, but in reality they have very little or nothing to do together. They are both foundational principles of Object-Oriented Programming, but that's about all they have in common, so let's break each one down individually, as understood by a five-year-old.

Encapsulation

This is exactly what is sounds like: encapsulating your stuff so that no one else can see it or touch it without your explicit approval.

As a five-year-old, you have just started kindergarten, and one of the best times of the day is lunch time, of course! You, like many of the other kids in your classroom, brought your lunch with you in your favorite lunchbox. As you place your lunchbox on the shelf with all the others, you just can't help but wonder what lunches some of the other students brought. You see lots of colorful lunchboxes, a few brown paper bags, and even just a plain-old grocery bag, but you can't see inside any of them, so you have no way of knowing what anyone else brought for lunch without going and asking them directly.

And while this can be annoying at times, having to talk to everyone to know what they brought, it is a very good thing that your lunches are all wrapped up nicely. It means that the bullies can't just come and mess with your lunch without you knowing about it. You saw your mom pack your lunch, you know everything that is inside your lunchbox, so it is pretty obvious is anyone else has taken anything, and you can go tell the teacher if you notice anything missing.

Polymorphism

This one is a bit harder to understand as a five-year-old, but the basic idea is that you can build complex "types" out of smaller, simpler "types", or that there can be multiple types that can do similar things. A "type" is just an abstract concept of some "thing", and is usually represented as an encapsulated Object. The object has some internal properties to it, and a public interface that allows access to that data, and that interface can be shared by many different "things".

Going back to the example above, lets say that one day the cafeteria is serving pizza, but a boy in your class can't eat cheese, and so he starts looking to trade lunches. So he starts talking to all the boys and girls that brought their lunch to see if any one of them wants to make a deal. He knows that both the food served in the cafeteria and the lunches in all the lunch bags are edible, but that one of these lunches will probably be better for him than the others. In this situation, the public interface is the fact that he can eat a given lunch, but there are many different types of lunches that could be eaten. The pizza in the cafeteria is a very different lunch than the PB&J sitting in your lunchbox, but because they are both lunches, they can both be consumed in similar ways.

Another way to look at this is through polymorphic inheritance. You know what a sandwich is, and you know that your PB&J is a specific type of sandwich. Anything that any other sandwich can do can also be done by your PB&J, namely holding stuff between 2 pieces of bread, it just happens that your specific type of sandwich holds peanut butter and jelly between its slices.