Member-only story
Python: Pillars of Object-Oriented Programming

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.
- Inheritance
- Encapsulation
- Polymorphism
- 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 classclass 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
…