Thinking Objectively: OOP Basics
Tags: codenewbie, oop
Published: 2022-07-24 22:24:35

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:

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

```

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:

```ruby

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

```

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

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

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

```ruby
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

``` 

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! 

```ruby
class Duck 

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

end

Duck.new(name: 'Cleopatra', color: 'brown', age: 2, gender: 'male', available: true)

#etc...
```

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

![car as OOP class](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/lao99thuiq0yephbyuxy.jpg)
# 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.