top of page

The Four Pillars of Object-Oriented Programming

  • Writer: Aanya Verma
    Aanya Verma
  • Aug 12, 2022
  • 3 min read

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 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.

Comments


bottom of page