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.