4 Pillars of OOP: Object Oriented Programming

There are many programming languages based on OOP, Object Oriented Programming, such as Java, Python, Ruby and a lot more…

Object Oriented Programming allows objects to reflect real life entities. For example, a table has a property of 4 legs and an action of supporting things. It is a huge benefit for programmers to treat objects in programming like real entities.

In OOP, there are 4 core principles. I would like to talk about these 4 concepts using Python and I will make it easy to understand for beginners.

  • Abstraction
  • Encapsulation
  • Inheritance
  • Polymorphism

These are 4 pillars for OOP and you may have heard of them.

Before getting started, I should go over .

“Object” and “Class”, what are they?

What is the relationship between these two?

As it sounds, object is an entity like anything you think of, a car, a laptop, a table etc. Class is just like a blueprint for object. It has all necessary properties to create an object.

Let’s say you want to make a table, you need a blueprint that contains the height, width etc. A table is made using its blueprint.

In programming it sounds like this.

An object is instantiated using a class.

It is the same as, “A table is made using a blueprint.” So, instantiation is creating an object using a class. The created object is an instance of the class.

Abstraction is referred to as hiding unnecessary details and giving access to only what’s necessary.

A user doesn’t need to know how things are implemented under the hood.

Just like making a phone call, we just need to hit the dials. We don’t know how the call is made and we don’t need to know if you just want to make a phone call.

Let’s take a look at this code snippet below.

If you are not familiar with terminologies; attribute and method, an attribute is a property and a method is a function in a class.
In Animal class, num_legs is an attribute and walk is a method.

In example 1, walk method is called and it prints “Animal is walking using its 4 legs.” As a user who just needs to call the method and get the output, the logic behind the method isn’t important.

Also, in example 2, I want to know how many times integer 2 appears in [1, 2, 2, 3] by using count method. I don’t need or want to know how the method is implemented when I just need the output.

Hopefully these examples help you understand what abstraction concept is.

Encapsulation, just like how it sounds, is to encapsulate data for those reasons below. Here, data means attribute or method. (remember?)

  • Keep data private or protected
  • Bundle up all the related data to be organized

Let’s have a look at the code snippet, it is easier to understand with actual code.

We have Animal class with age attribute and it is private, so accessing it from outside of the class will raise attribute error. But if age attribute is only to display not to be modified, to be protected in other words, we can utilize a getter method which is get_age method in the code.

By the way, Can you find any functions or variables outside of Animal class? There is no function outside of the class because all the functions and variables are encapsulated in the class. We don’t pollute the global scope with a random function or variable that doesn’t belong to anywhere, our code is well organized.

Inheritance, the name tells what it is, is about subclass(child class) inheriting properties of superclass. (parent class) It is something that you might be already using without noticing.

As you can see the code above, Dog class extends Animal class and Dog class has all the properties available from its superclass, Animal.

Notice there is no go_walk method defined in Dog class. However, rocky, an instance of Dog class, has access to the method(line 21) as well as name attribute(line 18), which is set for Dog class on line 10, using super().

These are all done implicitly through inheritance concept in OOP.

Polymorphism, the last concept of this article and it sounds complicated but it really isn’t. Just think of these 3 different Michael Scotts are from the regional manager Michael Scott. (Hopefully, you’ve watched the office.)

Not a problem even if you haven’t watched the office. It is quite easy to understand when you look at the code. I will keep using Animal and Dog classes to make it easy.

Please notice there is go_walk method within Dog class and it overrides go_walk method inherited from Animal class. When line 21 is executed, go_walk in Dog class is executed, as you see it prints “woaf, Rocky wants to go for a walk!”

With polymorphism, a superclass and its subclasses can have a method with the same name, but they all can have different tasks to do. Of course, a subclass can access to the method from its superclass, but it is out of scope for this article.

Thank you for reading and hopefully I was able to help you to have a better understanding of the 4 core principles of OOP; Abstraction, Encapsulation, Inheritance and Polymorphism. I tried to keep it simple and easy.

There is a lot more to learn, of course, it is just a tiny part of OOP. Let’s keep learning and keep pushing ourselves and we will make it there.

Wish you all luck.

Jeff Kim






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
Jeff Kim

Jeff Kim

💻 Software Developer | 🛠 Licensed Automotive Technician | 🏂 Part-time Snowboarder