DEV Community

James Macapagal
James Macapagal

Posted on

You Down with O-O-P?

Object-oriented Programming.

Sounds so fancy, huh?

It sounds like something out of Star Trek:

DATA: My neural network is fluid, whereby thoughts are attributes of a certain class and subroutines stored like methods.
LA FORGE: Not unlike object-oriented programming?
DATA: Precisely.

data

(I'm a huge TNG fan, so come at me with the Trek)

It's a term that gets thrown around a lot, and it sounds like something super technical.

(Anecdotally, my wife (who is not a programmer), listens to a Sleep Podcast which is just some dude reading Wikipedia articles. Object-oriented programming was one of them.)

sleep

But, as I wrap up Phase 3 (of 5) in Flatiron School Bootcamp, I want to attempt to explain object oriented programming to both my wife and to the five people who will invariably read this 9 years from now.

An ELI5 (explain like I'm five) if you will!

So, come along, stay for the memes and gifs, and really, maybe OOP was just the friends and lessons we learned along the way...


What the heck even is Object-oriented programming?

At its most basic, Object-oriented program is a paradigm- a way of programming. It's a way of specifically programming objects. So, quite literally, programming that is oriented toward the programming of objects.

That's it.
Blog over.
Pay me $150,000, Mark Zuckerberg!

(Just kidding!)

While this definition sounds simple, the concept of it is what is more important (and consequently harder to grasp).

In layman's terms, we want to build things to build other things for us. This is better than building something from scratch every single time we need them.

Because, while we can program objects, it is what we do with those objects is ultimately what's most important.

Before I get deeper into the technical aspects, like my hypothesis states, I want to give ELI5 example of OOP.

So... let's begin...


Legos... My Favorite Toy!

When I was a little kid (and arguably, still am...), my favorite toys were Legos (and video games, which, I'll get to!).

Now, if you've never built a Lego, it's literally pieces of plastic bricks and a set of instructions in a box. You take out the pieces and they're in no order particular order. You read the instructions, and the instructions are neatly laid out in the following format:

  1. An artist rendering of the final product.
  2. A page showing all the pieces included in the box.
  3. A step-by-step guide showing you literally where to place each and every piece.

Patiently follow the instructions (and trust that they are right!), you'll eventually build the Lego!

Let's break everything down again, because I promise I will tie this back to Object-oriented programming.

In every box of Legos, there are:

  • An idea/picture of what this particular lego model is
  • Lego pieces.
  • A step-by-step instruction manual on how to build the Lego.

This makes it easy for any kid (or adult) to pick up a box of Legos and build it without any additional instructions.


The Opposite of Legos

Now, let's think of the reverse of what this process might be.

How annoying would it be for Lego if they didn't include that guide?

Every customer would either need to figure it out on their own just using the picture (cough K-NEX cough), or, perhaps even more annoyingly, ask Lego for instructions every single time.


OOP: Built To Last

So, let's regroup.

Object-oriented programming is a philosophy/paradigm of programming oriented toward the coding of objects, and we can use those objects to, well, build things!

Furthermore, we can create that code to create more code.

This is better than the reverse of that, which is quite literally, building things from scratch, every single time.

In (most) Object-oriented programming languages, programmers begin by writing classes, which act like blueprints for the creation of objects. Every time that blueprint runs, a new instance of that object gets created!

To tie back to my Lego example, think of a particular type of Lego model as a class, and every Lego box that gets created is an instance of that object.

For instance (no pun intended), I always loved the Lego trains. There was one particular model that I got for a birthday. It was a black and yellow one fit with a dining car, sleeper car, and, of course, a working engine car that propelled the train!

I was so excited. I had the bestest, most amazingest Lego train ever!

But... I got a rude awakening the next day at school. It turns out another kid in my school got the same Lego train model for their birthday!

gasp

Now... as much as I want to believe I was a unicorn-child that got the onlyest Lego toy in the whole wide world, I'm not idiot (probably).

My Mom probably went to the toy store, went to the Lego section, and picked a Lego train models off the shelf. My classmates parents probably did the exact same thing with the exact same Lego train model.

That lego Train model is a class - quite literally a model of this particular line of Lego trains. Each box of that model is an instance. Assuming every kid builds the train properly, the train will look identical to other kids who have the exact same one because of the instructions inherent to that toy.


Lego Island

Well, let's talk about one of my other favorite toys: video games.

And one of my favorite video games was Lego Island.

Lego Island

It's an old PC game that, you guessed it, is about being on an Island of Legos!

So, applying our new understanding of Object-oriented programming, Lego Island was probably built using Object-oriented principles.

Quite literally, the Legos in that game are their own classes. Each of those instances are the renderings of those classes.

The water. The Lego people. The buildings.

And each of these objects can interact with one another to provide for the interactive gaming experience that makes video games, well, fun!


Conclusion

So, why does this matter?

Object-oriented programming, while a bit harder to grasp initially, actually makes programming much more elegant and sustainable.

Moreover, it is the foundation for many of the most useful programming languages, such as Ruby, C++, Python and more.

While each language will have it's own syntax, Object-orientated programming sets the foundation for many programming languages, which ultimately makes it easier to learn and use them.

you down with oop?


Sources:
https://en.wikipedia.org/wiki/Object-oriented_programming

Top comments (0)