Geek Culture
Published in

Geek Culture

The Four Pillars of Object-Oriented Programming

Photo by Duncan Kidd on Unsplash

Object-Oriented Programming (OOP) is a programming paradigm based on the idea of “objects”. A programming paradigm can be thought of as a set of rules that programmers follow when writing code to help accomplish a certain task. The four pillars of OOP are those set of rules. These pillars represent the design principles that help programmers write clean code.

The four pillars are:

  • Abstraction
  • Encapsulation
  • Inheritance
  • Polymorphism

Today I am going to talk about each of these principles. Let’s take a look.

Abstraction

Abstraction is to hide the implementation details of a process. Think of the process of making pizza. There are a few steps you would have to take in order to complete the process. Some of these steps might be

  • Roll out the dough
  • Cover pizza dough with sauce
  • Grate the cheese
  • Cover pizza with cheese
  • Preheat oven
  • Put pizza in the oven

Now imagine if you had to make pizza multiple times a day for multiple people. Wouldn’t it be better if we could could just press a button that did the whole process? That is what abstraction is about. If we think about it in code we could define a function with all the instructions in it and then we can reuse that function to make things a lot easier. For example the code could be something like this.

def makePizza 
puts "Rolling out pizza dough"
puts "Covering pizza dough with sauce"
puts "Grating the cheese"
puts "Covering pizza dough with cheese"
puts "Preheating oven"
puts "Putting pizza in the oven"
puts "Pizza is ready!!!"
end

Now we don’t have to worry about what the process of making pizza is like. All we have to do is run this function and the job is done. This is an amazing way to make code reusable. Since you don’t have to understand the process it also makes it easier for other developers to read your code faster. If we had to understand every single process in the code there would be no work getting done. Now the next pillar shows us a way of achieving abstraction.

Encapsulation

Encapsulation refers to the hiding of data. This can be done by removing access to parts of the code and making things private. You can make things more inaccessible if those things aren’t needed. Encapsulation is used to hide the state of a structured data object inside a class, preventing unauthorized parties’ direct access to them. So why would we prefer this kind of privacy over making everything global. Well, there are several reasons.

  • A completely different part of your program cannot unexpectedly make changes to the data inside the object.
  • The functionality is defined in one place and one place only.
  • You will not end up with Spaghetti Code.

These are just a few reasons why encapsulation is very important in object-oriented programming. We hide away the data in a place nothing else needs access to, and only reveal what is needed. That is what encapsulation is all about.

Inheritance

Inheritance let’s one object acquire the properties of another object. If we know of something that is needed in multiple places with maybe some small differences we can use inheritance to improve re-usability. Inheritance is usually used with a parent and child relationship between classes. We try to make the code we create as related to each other as possible. For example if we wanted to create animals we know that most animals move around the world by walking so we could have some code that looks like this.

class Animal  def initialize
puts "This is an Animal"
end
def movement
puts "The animal is walking"
end
endclass Dog < Animal def initialize
puts "This is a Dog"
end
enddog = Dog.new
dog.movement // Will puts "The animal is walking"

In this our dog class inherited the movement method from its parent. So when we call the movement method on the dog object we will get the same behavior of the movement method on the animal class. The main benefit of using inheritance is that your code can be re-usable. We can avoid writing the same code over and over again.

Polymorphism

Polymorphism means that objects in the same inheritance chain can appear in different forms and do different things. We can use the same animal class example to showcase what that means. Let’s say that now we want to create another class of animal like a snake. Snakes may have many similarities with other animals but their movement is definitely not like many others.

class Animal  def initialize
puts "This is an Animal"
end
def movement
puts "The animal is walking"
end
endclass Snake < Animal def initialize
puts "This is a Snake"
end
def movement
puts "The snake is slithering
end
endsnake = Snake.new
snake.movement // Will puts "The snake is slithering"

As you can see we were able to override the movement method to make it more specific to what a snake would actually do. This lets us create objects that inherit some things from a parent class but also modify some things to make it a thing of its own.

This is a very simple explanation of each of the four pillars of object-oriented programming. Following these rules has helped many programmers write clean object-oriented code. Really understanding these principles will make you a better developer. Have fun diving deeper and using these principles in your code. Happy Coding! 😎

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store