The Pillars of Object Oriented Programming

Introduction

In the previous article, we covered an introduction to Object Oriented programming as a well as it being one of the most widely used paradigm for programming languages. In this article, we focus more on the pillars or concepts of Object Oriented Programming, giving more explanations to each one of them and making use of examples to better define them.

OOP was introduced in the 1960’s by Alan Kay. Software developed back then had small scalability and as such Object Oriented Programming wasn’t the best solution but as the complexity of software and real life applications improved, OOP principles became a better solution. The four main pillars of Object Oriented programming are:

Encapsulation

The idea of encapsulation is to have methods and variables within the bounds of a given unit. In Python, this unit is called a class and the members of a class are locally bound to that class. Encapsulation helps better understand and establish the global and local scope.

For example, a company will of course have different departments and sections where you will be required to deal with the data and operations for each of them separately. Classes and objects help in encapsulating and in turn restricting the different functionalities.

Another benefit of encapsulation is that it is used for holding data and its internal representation. This is called information hiding. Access modifiers represented by keywords such as public, private and protected are used for information hiding. In Python though, the use of single and double underscores substitutes for that.

Polymorphism

This refers to something that can have many forms, so when we talk of polymorphism in Python, it refers to the ability of various objects of any class to take various forms. For example:

num = 8

sequence = [1,2,3]

name = “ayo”

new_num = num * 2

new_sequence = sequence * 2

new_name = name * 2

print(new_num, sequence, new_name)

The output of this code will be:

16,[1, 2, 3], ayoayo

As we can see from this example, the same operator() performs differently in three cases showing polymorphism.

Inheritance

Inheritance encompasses the relationship between the parent class and its child class. For example:

class parent:

members of the parents class

class child(Parent):

additional members of the child class

When the structure of inheritance gets complicated, Python adheres to something called the Method Resolution Order(MRO) that determines the flow of execution and also helps in determining the scope of the different members of the given class.

Abstraction

The core of abstraction in Python is the implementation of something called abstract classes and methods, which can be implemented by inheriting the “abc” module. The “abc” module simply means an abstract base class. This class is first imported and then used as a parent class for some class that becomes an abstract class.

Conclusion

We now know more about the main pillars or concepts of Object Oriented Programming and how they are used in Python. In the next article, we will learn more about the various classes and instances we have in Python.

Olamide Ayeni

Share
Published by
Olamide Ayeni

Recent Posts

Building A Message Board App With Django

Introduction In this article, we shall use a database for the first time to build…

6 days ago

Building A Two Paged Web Application Using Django

Introduction In this article, we will build a pages app that has a homepage and…

2 weeks ago

Creating Your First App In Django

When you say you want to use Django, it means you want to build a…

3 weeks ago

Creating Your First Project In Django?

Introduction Django is a high level Python framework that encourages rapid development and clean, pragmatic…

4 weeks ago

Django Setup For Windows

Django Setup To work with Django, we have to install Django first. Now when you…

4 weeks ago

Why Choose Django When There Are Other Frameworks For Python?

Why choose Django when you have other frameworks for Python? Django, Django, Django. When it…

1 month ago