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:
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.
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:
As we can see from this example, the same operator() performs differently in three cases showing polymorphism.
Inheritance encompasses the relationship between the parent class and its child class. For example:
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.
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.
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.
Introduction In the fast paced world of technology, learning a versatile and high-in-demand programming language…
Introduction User Authentication policy is a very crucial process for every application and organization. It…
Introduction In previous articles, we have learnt about Django, how it works and how we…
Introduction In this article, we shall learn how to build and implement a blog app.…
Introduction In this article, we shall use a database for the first time to build…
Introduction In this article, we will build a pages app that has a homepage and…