6 min read

I just did a quick Google search, I’m not the first one to think I’m clever. But I couldn’t think of a better name.

So a little background on me. I’m still a beginner and I’m still learning a lot. I’ve been a Software Developer for about a year now and some basic concepts are just starting to become more clear to me. So I’d look at this as a very beginning intro to OOP and hopefully you’ll be so interested by what I have to say you’ll explore further!

What is OOP?

OOP stands for Object-Oriented Programming. Why would you want to learn about this concept? If done correctly, it makes code more maintainable in the long run. It’s easier to follow and easier for another dev to jump in to the project, even if that other dev is yourself in the future.

Huh? What’s the Difference?

Object-Oriented Programming vs Functional vs Procedural. I’ve always understood each of these concepts by themselves but not how they fit in with each other or what the advantages of choosing one type over the other. These are not the only programming paradigms out there but they are very popular paradigms.

Before we can really dive into OOP it’s important to understand how it differs from other programming paradigms.

Object-Oriented: Object-Oriented programming is a type of abstraction done by using classes and objects. Classes define a data structure and can send and receive messages and also manipulate data. Objects are the instances of these classes.

Functional: Functional programming is a set of functions. These functions take input and put out output, ideally there is no internal state that would affect the output of an input. This means no matter what input you put in, you should always receive the same output.

Procedural: Procedural is probably what you first learned. This involves telling the computer a set of steps that it should execute.

Polymorphism

It’s morphin’ time! I’ve heard this word a lot but I was too nervous to ask what it meant. It’s okay, I’m here to help you out.

This is a concept found in Object-Oriented Programming, it’s a way to have similar objects act differently.

The classic example is using a class Animal. You can have many different animals and they can all have the method speak. However, cats and dogs will say different things when they speak. so dog.speak() would result in ‘woof’ while cat.speak() would result in ‘meow’.

Inheritance

Objects can inherit features from other objects. If you have an object that shares a lot of similar functionality as another object but varies slightly, you might want to use inheritance. But don’t abuse inheritance, just because you can do it doesn’t mean this is something you should be using everywhere. OOP is used to help make more maintainable code, if you have classes inheriting all over the place with each other, it ends up making your code less maintainable.

Encapsulation

Encapsulation is a fancy word describing private methods and variables. These methods and variables should only be avaliable to the class they belong to, which helps to make sure that you aren’t doing anything with a different class that will have unintended results. While in Python there is no actual privacy, there is a convention to help prevent you from using something you shouldn’t be touching. The convention is prepending variable names with an underscore.

Python!

So now you have a little background on the basics of OOP, what does this look like in the real world?

I’ll use Python as an example in this case. Python is a multi-paradigm language that is great for object-oriented programming and that’s fairly easy to read, so even if you haven’t seen any Python in your life before, you should be able to get a handle on what’s going on.

So we’re going to make a zoo. Zoos have animals, so in order to create an animal, let’s start with an Animal class.

class Animal():
   def __init__(self, name):
       self.name = name

All we’ve done here is make an animal class and set the name. What can differ between animals besides their name? Number of legs? Color? The food they eat? There are so many things that might be different but let’s just pick a few. Don’t worry too much about syntax. If you like the look of Python, I recommend checking it out because Python is fun to write!

class Animal():
   def __init__(self, name, legs, color, food):
       self.name = name
       self.legs = legs
       self.color = color
       self.food = food

Now I can make any animal I want.

dog = Animal('dog', 4, 'black', 'dog food') cat = Animal('cat', 4, 'orange', 'cat food')

But maybe we should take care of these animals? All of the animals at this zoo happen to get hungry based on the number of legs they have and how long it’s been since their last feeding. When their hunger levels reach an 8 on a scale of 1-10 (10 being the hungriest they’ve ever felt in their life!), we’ll feed them!

First we need to allow each animal to store its hunger value.

class Animal(): def init(self, name, legs, color, food, hunger): self.name = name self.legs = legs self.color = color self.food = food self.hunger = hunger

Now we can add a method (a name for function that belongs to a class) to the class to see if we should feed the animal and if we need to feed it, we’ll feed it.

class Animal(): def init(self, name, legs, color, food, hunger): self.name = name self.legs = legs self.color = color self.food = food self.hunger = hunger
  def time_to_feed(self, hours_since_meal):
      self.hunger = 0.3 * (hours_since_meal * self.legs) + self.hunger

      if self.hunger >= 8:
          print('Time to feed the ' + self.name + ' ' + self.food + '!')
      else:
          print(self.name + ' is full.')
dog = Animal('dog', 4, 'brown', 'dog food', 8) dog.time_to_feed(8) cat = Animal('cat', 4, 'pink', 'cat food', 2) cat.time_to_feed(2)

Ok but why is this good? Well let’s say I didn’t do this in an object-oriented manner. I could do it this way:

dogName = 'dog' dogLegs = 4 dogColor = 'brown' dogFood = 'dog food' dogHunger = 8
catName = 'cat' catLegs = 4 catColor = 'pink' catFood = 'cat food' catHunger = 2

Before when I wanted to add a hunger level, I just needed to add more parameters to my __init__ method. Now I need to make sure I’m adding in parameters to each object individually. I’m already tired. I could put those all in arrays but then I’m relying on the order of multiple arrays to always stay in the order I expect them in. Then I create the same time_to_feed function. But now it’s not clear what time_to_feed is being used for. A future developer joining this project might have hard time figuring out that you meant this for your animals.

I hope you enjoyed this little introduction into Object-Oriented Programming.

If you want to jump into learning more JavaScript, why not start by finding out the difference between mutability and immutability? Read on now

About the author

Liz Tom is a Creative Technologist at iStrategyLabs in Washington D.C. Liz’s passion for full stack development and digital media makes her a natural fit at ISL. Before joining iStrategyLabs, she worked in the film industry doing everything from mopping blood off of floors to managing budgets.

LEAVE A REPLY

Please enter your comment!
Please enter your name here