eli5 what is object oriented programming? honestly eli5

1.52K views

eli5 what is object oriented programming? honestly eli5

In: Technology

9 Answers

Anonymous 0 Comments

To understand OO programming, you first have to understand procedural programming, because the former is built on the latter.

Procedural programming is like following a recipe (a procedure). Say you want to tell someone how to make bread. You can write a recipe by listing out the things needed to make it (these correspond to the data or variables in programming) and the steps you need to take (these correspond to the statements or functions in programming).

>**Bread**

>Ingredients:

>* Flour: 2 cups
>* Water: 1 cup
>* Salt: 1 tsp
>* Sugar: 1 tbsp
>* Yeast: 1 packet

>Stir sugar and yeast into warm water. Put flour and salt into medium mixing bowl. Slowly add the water to the flour, mixing as you add. Knead the dough for ten minutes. Let stand for 1 hour. Place on a pan dusted with flour and bake at 400℉ for 20 minutes.

Now, for object oriented programming, imagine a more complex recipe that takes several individual parts and puts them together, like a cake with icing. You could do this by making one big recipe, perhaps breaking it out into sections.

>**Cake**

>Ingredients:

>* Flour: 2 cups
>* Water: 2 cups
>* Eggs: 2
>* Butter: 8 oz.
>* Powdered Sugar: 1 cup

>Cake: melt 4 oz. butter. In a large bowl, mix melted butter, flour, 1 1/2 cups water, eggs, and 1/2 cup sugar until smooth. Pour into 8″ diameter round pan. Bake at 350℉ for 15 minutes.

>Icing: while cake is baking, melt the remaining 4 oz. of butter and mix with remaining 1/2 cup sugar and 1/2 cup water. Beat until creamy. Optionally, add food coloring.

>When cake is done and cooled, apply icing to outside of cake.

But there are downsides to that. The more complicated a recipe is, the more likely it is for you (or your readers) to get something wrong. Also, if you have two recipes that call for icing (say, cookies), now you have to write the icing section twice, once in each recipe.

So in object-oriented programming, you would break your parts out into separate full recipes, and just refer to them.

>**Cake**

>Ingredients:

>* Flour: 2 cups
>* Water: 1 1/2 cup
>* Eggs: 2
>* Butter: 4 oz.
>* Sugar: 1/2 cup
>* Icing (see p.2): 1 cup

>In a large bowl, mix melted butter, flour, water, eggs, and sugar until smooth. Pour into 8″ diameter round pan. Bake at 350℉ for 15 minutes. When done and cooled, apply icing to outside of cake.

>**Icing**

>Ingredients:

>* Water: 1/2 cup
>* Butter: 4 oz.
>* Powdered Sugar: 1/2 cup

>Mix melted butter, sugar, and water in a medium bowl. Beat until creamy. Optionally, add food coloring.

In object oriented programming, each recipe corresponds to something called a “class”, a self-contained piece of code that describes what an object is and how it’s made. An “object” or “instance” of a class corresponds to the dish that the recipe describes. When referring to an object in code, you don’t need to know how that object works or how it’s made. You only need to know what you can do with it, just like you can use either homemade or store-bought icing on a cake, and you can use icing on either cakes or cookies. This concept of objects taking care of themselves and not depending on other objects is called “encapsulation”, and while it’s especially important in object-oriented programming, it’s actually used in one form or another in nearly all types of programming.

PS.-I’m not a baker, these recipes are just for illustration. They’re probably very wrong.

You are viewing 1 out of 9 answers, click here to view all answers.