Towards Dev

A publication for sharing projects, ideas, codes, and new theories.

Follow publication

Python: Pillars of Object-Oriented Programming

Sunil Kumar
Towards Dev
Published in
5 min readAug 21, 2022

A programming paradigm is an approach to solving a problem using some programming language. We can also say it is a method to solve a problem using tools and techniques available to us following some approach.

As per wiki documentation, OOP is a programming paradigm based on the concept of “objects”, which can contain data and code: data in the form of fields, and code, in the form of procedures (methods). A common feature of objects is that procedures are attached to them and can access and modify the object’s data fields.

In this python tutorial, we are going to learn about the four pillars of OOP or Object Oriented Programming.

  1. Inheritance
  2. Encapsulation
  3. Polymorphism
  4. Abstraction

Inheritance

Inheritance allows you to define a class that inherits methods and properties from another class. The base class is called the parent class, and the derived class is known as a child.

In inheritance, the child class acquires all the data members, properties, and functions from the parent class. Also, a child class can also provide its specific implementation to the methods of the parent class.

Syntax

class BaseClass:
Body of base class
class DerivedClass(BaseClass):
Body of derived class

Let’s see an example.

# Base class
class Vehicle:
def can_horn(self):
return True
# Child class
class Bus(Vehicle):
def bus_info(self):
print('Inside Bus class')
# Child class
class Car(Vehicle):
def car_info(self):
print('Inside Car class')
# Create object of Car
bus = Bus()
# access can_horn using bus object
print(bus.can_horn())
bus.bus_info()

As we can see in the example, we specify a class inherits the members of another class by writing the parent class within the parenthesis of the child class. And a child class possesses all the methods and variables as defined by the Base class. That is why we can use the can_horn() method in an object of Bus class because the Bus

Create an account to read the full story.

The author made this story available to Medium members only.
If you’re new to Medium, create a new account to read this story on us.

Or, continue in mobile web

Already have an account? Sign in

Published in Towards Dev

A publication for sharing projects, ideas, codes, and new theories.

Written by Sunil Kumar

With an extensive professional experience spanning over 16 years in the IT industry, I am a seasoned expert in AWS Cloud, DevOps, FastAPI, and Python.

No responses yet

Write a response