Python: The four pillars of Object-Oriented Programming

Benjamin Bennett Alexander
CodeX
Published in
6 min readJul 25, 2022

--

Photo by Oliver Sjöström: https://www.pexels.com/photo/photo-of-woman-wearing-abaya-1122407/

Python is an Object-Oriented Programming language. Everything in Python is an object. Like other Object-Oriented languages, when creating objects using classes, there are four(4) basic principles for writing clean and concise code. These principles are called the four pillars of object-oriented programming (OOP). These four pillars are Inheritance, Polymorphism, Encapsulation and Abstraction. In this article we are going to explore these four pillars of object-oriented programming.

Inheritance

In Python it is possible to create an object that inherits the methods and properties of another object. This is called inheritance. In inheritance, there is a parent class and a child class. A child class inherits the properties and methods of the parent class. Here is an example of how inheritance is implemented when creating objects in Python.

In this code, you have the Animals class, which is the base or parent class; then you have the Dog class, which is the child class. You will notice that the child class takes the parent class as a parameter (The name Animals is between the parenthesis of the child class); this is basically how inheritance is implemented. The child class inherits the properties and methods of the parent class. In the code above, the child class has inherited the instance variables — name and age using the super() method. So, instead of us recreating these variables in the child class, we simply inherit from the parent class. Inheritance helps in creating reusable code and makes our code more concise.

Polymorphism

Polymorphism simply means ‘many forms’. In Python this means that you can have one function or object that can be used in different ways. Let’s take an example of the addition operator (+); we can use it to add numbers, but we can also use it to concatenate strings. Here are two examples below:

In OOP, polymorphism can be implemented with inheritance. The methods of the child class will have the same name as those of the parent class. The child class method overrides the method inherited from the parent class, when called with the child class object. This kind of implementation is called method overriding. Below, we have a parent class called Cars, and child classes that inherit from the parent class. Notice that all the classes (base class and child classes) have the same method called start. However, the start methods in the child classes have been modified to fit the ‘needs’ of that child class. So polymorphism in inheritance is useful if we need to modifify the parent method to fit the needs of the child class; while still maintaining access to the parent class method.

However, note that polymorphism can also be implemented without inheritance. This means that even though the classes are not explicitly linked to the parent class, we can still use the same name for the class methods, and the implementation will achieve the same results as above. See the modified code below:

Encapsulation

Encapsulation is the process of making data private by wrapping data and its methods in a ‘capsule’ or unit, so that it can not be accessed or modified outside of that unit. This is achieved by making variables inside a class private. In Python we can make variables private by prefixing the variable name with a double underscore ‘__’. In the code below, we have a variable called brand. We add a double underscore to the name (__brand) to make it private. Now, when we try to access the variable outside the function (print(car.__brand), we get an attribute error because the variable __brand is now private, and cannot be modified or changed from outside the class.

If you do not want to make the data private, you can make it protected. To make the variable protected, you only need to prefix the variable with a single underscore ‘_’. However, protected data can be accessed and modified outside the class. Adding a single underscore is simply a conventional way of informing other programmers that may use your code that the variable with an underscore prefix is protected and should not be modified.

Abstraction

The fourth pillar of OOP is Abstraction. Abstraction is about keeping the process simple by hiding unnecessary details from the user. Think of a car; the actual mechanism that keeps a car moving is hidden from the user. It is important to know how to drive a car; but it is not necessarily important to know what happens under the hood when you drive the car. Abstraction is about keeping the internal mechanics of the code hidden from the user. This reduces the complexity of the code, and ensures that we only concentrate on what is important.

In OOP abstraction is achieved by creating an interface class (base class) and implementation classes (subclasses). We can create an interface class using the built-in abc module. Below, we create an abstract class called Car.

In the code above, we create the abstract class (Interface class) with the help of ABC class from the abc module. The @abstractmethod is a decorator for the abstract methods. The abstract class and its decorated methods are for declaration only, and not for implementation. Remember this is only an interface class; the implementation classes will come later. You cannot instantiate an object of the abstract class; you will get an error. See below:

Now that we have created the abstract class, we can now create the classes that will implement the abstract class. The implementation classes will have the same method name as that of the abstract class. The methods in the implementation classes will override the method in the abstract class. In the code below, we have created the Tesla and BMW classes as subclasses of the abstract class. Here is a full code below:

So in the above code, the abstract class sets a standard that every car subclass should have a model — car_model (method). This is implemented by the subclasses (Tesla and BMW). Basically, the abstraction class ensures that the focus is on WHAT should be implemented (That is, every car should have a model) and not HOW it is implemented. The implementation is left to the subclasses.

Conclusion

These are the four pillars of Object-Oriented programming. Thank you for reading. Please share this article, clap, comment and subscribe to the mailing list if you are not yet a subscriber. You can also follow the author on Twitter and LinkedIn.

………………………………………………………………………………………

Python Tips Tricks . Click here. 50 Days of Python: A Challenge a Day. Click here

--

--

Benjamin Bennett Alexander
CodeX

Tech ( Python, Data science and Blockchain) and Finance enthusiast. I just share stories.