The Four Pillars of Object-Oriented Programming

The four pillars of OOPS in Python are: Inheritance, Polymorphism, Data Abstraction, and Encapsulation. Let’s dive into further details about each of these pillars. 

If you are here, chances are you want to widen your knowledge on the concepts of  object-oriented programming!  

 

Python is considered an object-oriented programming pattern which uses classes and objects. By using OOPS in Python, a programmer can develop any type of application he/she desires. 

 

In this approach, objects denote certain real-world entities such as pencil, book, etc. The OOPS  programming paradigm focuses on writing a reusable code with widespread techniques through object creation. 

 

The four pillars of OOPS in Python are: Inheritance, Polymorphism, Data Abstraction, and Encapsulation. Let’s dive into further details about each of these pillars. 

  1. Inheritance 

Inheritance can be defined as a process of inheriting certain properties of a parent class to a child class. The existing class is known as the parent class and the newly created class is regarded as a child class or subclass. 

 

Inheritance is said to be an important concept in OOPS in Python because it defines the reusability of a code. Due to inheritance, we can use the parent class to generate a subclass instead of creating a new one from scratch. 

 

A subclass acquires all the functions, properties, and data members from the parent class. It also has the ability to provide specific implementations to the inherited methods of the parent class. 

 

For example: If we were to correlate inheritance with a real-life scenario, then think of cars as a subclass and a vehicle as the parent class. You can create a brand-new car by inheriting the functions and properties of a vehicle. 

 

These include - fuel tank, wheels, engine, colours, design, etc. On top of it, the car can also have certain extra properties as deemed fit by you. 

 

There are five types of inheritance in Python that you must know about.

 

  • Single inheritance: In this type, a subclass inherits its properties from only one parent class. 
  • Multiple inheritance: In this type, a subclass inherits its properties from multiple parent classes. 
  • Multi-level inheritance: In this type, there is a formation of a chain of classes. For example, imagine there are three classes - X, Y, Z. X is regarded as the superclass, Y is called the subclass of X, and Z is the subclass of Y. 
  • Hierarchical inheritance: In this type, more than one subclass is generated from a single parent class. 
  • Hybrid inheritance: Like the name suggests, hybrid inheritance is a combination of above-mentioned types of inheritance. 

 

  1. Polymorphism 

Polymorphism in Python can be defined as the ability of any object to take multiple forms. In layman terms, polymorphism allows a programmer to perform a particular action in multiple ways. 

 

For example: A woman named Shrishti is an employee at work. However, she’s also a wife, a daughter, and a mother at home. She takes on different roles and responsibilities in different situations.

 

Depending on the data type or the class type, a method in polymorphism can be used to process objects differently. Polymorphism is mostly used in collaboration with the concept of inheritance in Python. 

 

Using method overriding, a programmer can define the methods in a subclass that will share the same name as the superclass methods. Method overriding is nothing but re-implementing an inherited method in the subclass or child class. 

 

Method overriding is advantageous when you want to enhance the functionality in a case where the method originally inherited from the superclass does not fulfill the requirements of a child class. 

 

It is also useful in a situation where a child class wants to redefine the inherited function in case of multiple inheritance patterns. To execute the most appropriate method when called for it, Python first checks the class type of the object in-hand.  

  1. Encapsulation 

Like the name suggests, encapsulation refers to the concept of combining data elements and methods within one single unit. Class creation is a typical example of encapsulation because it combines all the data members into a single unit. 

 

Encapsulation is widely used to perform the task of information hiding wherein we can mask the internal representation of an object from outside. 

 

Moreover, encapsulation also protects the code from accidental modification by generating private methods and data members within a class. It also allows a programmer to restrict accessing the methods and variables directly from outside of class.  

 

For example: suppose there’s a variable in your code that is not visible from outside of the object. You then bundle the variable with methods that provide write or read access to it. In such a case, you have the power to mask specific information of the internal state of the object. Therefore, this is a way to protect the data stored in an object from other code objects. 

  1. Abstraction 

Abstraction can be defined as a procedure of handling the complexity of a given program by hiding any unnecessary information from the end-user. 

 

It allows a programmer to hide all the unnecessary information about the program from the end-user thereby reducing its complexity and enhancing program efficiency. 

 

Let’s try to understand abstraction with a real-world example. 

 

For example: You have just bought a brand-new phone. You’ll get an instructions module along with the packaging of the mobile phone. However, the information module does not contain any information about the internal working of the phone. 

Interview Questions Based on OOPS in Python 

OOPS in Python is a commonly-asked topic by employers during the technical rounds of the interview. Here are some of the interview puzzles you might be facing during the technical interview round: 

  1. What is the importance of OOPS in programming?
  2. What are the main features of OOPS in Python? 
  3. What are the benefits of using OOPS? 
  4. Differentiate between object and class. 
  5. What is the difference between compile time polymorphism and runtime polymorphism? 

Conclusion 

The key takeaways from the above article are: 

  • OOPS in Python makes the program super-efficient and easy for the user to understand. 
  • With the help of data abstraction, the data elements in the program code remain safe and secure. 
  • Programmers can write efficient code with polymorphism because it allows them to use the same interface for multiple objects. 
  • The code written can be reused because class is shareable. 

We hope that the above article has cleared your basics about OOPS in Python. You can expect a few interview puzzles based on this topic in the technical round. Therefore, ensure that you devote a significant chunk of your time preparing for it! 


Aanya Verma

3 Blog posts

Comments