DEV Community

Cover image for Thinking Objectively: OOP Basics
Eben Eleazer
Eben Eleazer

Posted on


Thinking Objectively: OOP Basics

After the last series on Big-O notation, I decided to do another series covering some of the basics of programming.

This time, we'll be exploring the 4 key concepts of Object Oriented Programming. Also known as the 4 "pillars", these concepts form the foundation of OOP. These concepts should help you think about coding in a way that can be very intuitive.

What are Objects?

It's self explanatory, but Object Oriented Programming (OOP) is going to be focused around the idea of objects. So, the first point of order is to understand what an object is in programming.

A Thought Experiment

In order to understand the idea of an object, let's do a thought experiment.

Think of an object. It can be anything you can imagine. Now, think about how you would describe your thing to someone else, without using its name.


Depending on your object, it was probably pretty simple. In fact, it's basically just charades on easy mode.

(It is small, I has a tail, It says "quack", it can swim, it has a bill, etc...)

Each of these descriptors can be thought of as either a property, or a behavior. Properties are things that describe what the object is, and behaviors describe what it does. Once you have enough of these descriptors, you can easily imagine the object (or something close enough).

In other words, an object can be thought of as a group of properties and behaviors.

We're going to come back to these properties and behaviors, but for now the big revelation is that they have to be thought of as a group!

Think about it, if I just say color: brown or can fly, individually they don't mean much. But when you group the properties and behaviors together you get the full picture:

   size = "small"
   tail = true
   quack = (do something)
   swim = (do something)
   mouth_type = "bill"
   color = "brown"
   fly = (do something)

Enter fullscreen mode Exit fullscreen mode

When written this way, it's easy to see how we can use this view of objects to start to model them using code.

Keeping it Classy

Let's make a small program about ducks. We'll start with two ducks:

duck_1 = "Cleopatra"
duck_2 = "Mr. Whiskers"

Enter fullscreen mode Exit fullscreen mode

That's cool, but let's say we want to tell the color of the ducks:

duck_1_name = "Cleopatra"
duck_1_color = "brown"
duck_2_name = "Mr. Whiskers"
duck_2_color = "mallard"

Enter fullscreen mode Exit fullscreen mode

We should also tell the age and gender of the ducks, and whether or not they're "available":

duck_1_name = "Cleopatra"
duck_1_color = "brown"
duck_1_age = 2
duck_1_gender = "Male"
duck_1_available = true

duck_2_name = "Mr. Whiskers"
duck_2_color = "mallard"
duck_2_age = 2
duck_2_gender = "Prefer not to say"
duck_2_available = true

Enter fullscreen mode Exit fullscreen mode

Now imagine, we have 10 more ducks sign up for our dating app (did I not mention what we were making?). We would have to write out those 5 variables for each one.

Not only that, but even though the values are different, all the ducks have a variable for name, color, age, gender and available. Since I know I need these variables for every duck, and I have a good idea what the possible values will be, It would be cool if I could just tell the program to make a "duck".

Classes allow us to do just that!

class Duck 

 attr_accessor :name
 attr_accessor :color
 attr_accessor :age
 attr_accessor :gender
 attr_accessor :available

end 'Cleopatra', color: 'brown', age: 2, gender: 'male', available: true)

Enter fullscreen mode Exit fullscreen mode

It might help to think of a class as a template or a prototype for individual objects.

car as OOP class

Object Orientation

With objects and classes we can represent real world (or imaginary) objects in code. By building onto, extending and connecting these objects and classes of objects to each other, we can build incredibly complex programs and model any number of situations.

Top comments (0)

Advice For Junior Developers

Advice from a career of 15+ years for new and beginner developers just getting started on their journey.