Thinking Objectively: OOP Basics

eben_eleazer

Eben Eleazer

Posted on July 24, 2022

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

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

#etc...
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.

💖 💪 🙅 🚩
eben_eleazer
Eben Eleazer

Posted on July 24, 2022

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related

Entendendo Herança e Composição
typescript Entendendo Herança e Composição

February 27, 2023

OOP Inheritance aka "It's About Family"
beginners OOP Inheritance aka "It's About Family"

August 14, 2022

OOP Encapsulation aka Wrap it Up
codenewbie OOP Encapsulation aka Wrap it Up

July 31, 2022

Thinking Objectively: OOP Basics
codenewbie Thinking Objectively: OOP Basics

July 24, 2022

Design Patterns
java Design Patterns

June 18, 2022