DEV Community

Cover image for Tackling Object Oriented Programming
Maxine
Maxine

Posted on

Tackling Object Oriented Programming

Understanding object oriented programming can be a tough task, especially for beginners. Many new programmers are introduced to OOP through encapsulation, inheritance and polymorphism. And while these concepts are fairly simple once you understand their roots, the problem becomes exactly that. Where are the roots?

The software development classes I took prior to flatiron school helped to get me thinking like a programmer, but I still wasn't there. Those three terms above still seemed like concepts that would take me a month to understand. However, when you're taught about classes and objects beforehand, that month can turn into five minutes.(Thanks flatiron!)

So what is a class? How about an object?

In technical terms, an object is an abstract data type created by a developer. Which means it can be anything! It can be a dog, a person, a plant, you name it! Whatever said object is can then have behaviors and attributes. Objects are defined by using classes, so in a sense classes are like the blueprints of an object. Is it starting to click yet?

Well, let's use a plant (object) for an example. For my phase 1 final project at flatiron, I decided to create a plant finder cli program. It simply allows the user to enter the common name of a plant species, and returns the scientific classification of that said plant species. But how does my program know about each plant's classification? Because those plants are all objects! Each time a plant is passed into the program it becomes an object, it's initialized through my class Plant, which pulls the data from an api class that will later be sent back to the user. But how did I get my plant object to do this through my class Plant? By giving it behaviors! In my class Plant, I was able to pull out key values about the plant upon each instance of a plant. By defining methods for instance variables I created, I was then able to access these pieces of information and connect it to my class CLI, which displays the information to the user. Did I lose you there?

Once we see that each object is it's own being, that can carry out functions (literally and figuratively), the words encapsulation, inheritance and polymorphism start to make a little more sense. When we encapsulate code, we're really doing what the word says, putting something inside of something else. We can encapsulate classes inside classes, methods inside classes, etc. And when we encapsulate those methods or objects, they won't be accessed until the code encapsulating them is actually ran. So back to my plant object.

Alt Text

I have a method called self.find_by_common_name, without going through the Plant class, I cannot call this method. I can try a thousand different ways and it won't work, and that's because it's encapsulated in my Plant class. The only way that method will work is if I pass in a common name through my Plant class. Making sense? Now imagine trying to understand all that without understanding what objects and classes are. No thanks!

Inheritance is also exactly what it sounds like. One class inheriting from another. Unfortunately, I did not use inheritance in my Plant Finder, so we will have to revert to another example for that. In this case let's have a Mom class and a Daughter class. In the Mom class you may have some methods that give the mom behaviors, such as kind, gentle, and funny.

Alt Text

When Mom has a daughter (let's call it class Daughter), these behaviors pass down to her. By using the syntax:

class Daughter < Mom

You're telling your program to take all those behaviors you defined in your Mom class and put them in your daughter class as well. This is a lot easier and more organized than copying all of that code over. And what if you make a change to class Mom? Using inheritance there's nothing to worry about, but if you copied it over you'd have to change it in both places! Along with inheriting all of her Mom's characteristics, Daughter class may have some of her own, such as being energetic.

Alt Text

You can add this in and nothing will be changed in Mom class, only in the daughter class. Pretty cool right?

Lastly we have polymorphism. This simply means that a class can have different characteristics at the same time. We have our Mom and Daughter class, let's put in a Talk class.

Alt Text

When we pass our talk method defined in both the Mom and Daughter classes as arguments in a method within the talk class, it calls on Mom and Daughter to talk, and we receive different outputs. The Mom class may output "Hello, how are you?", while the Daughter class may output "Oogo gagah" (assuming Daughter is a baby who can't speak yet). Do you see how we called on one class and got multiple responses, that's polymorphism!

Phew! That was a lot, but we did it! We started building the foundation to become awesome object oriented programmers. The most important part in understanding OOP is practicing doing it, while my experience creating a cli was helpful, I still have long ways to go.

Top comments (0)