Skip to main content

Object-Oriented Programming (OOPs) in Python

Python is an object-oriented programming language, which means it supports concepts like classes, objects, inheritance, polymorphism, encapsulation, and abstraction.
OOP allows developers to structure programs so that properties and behaviors are bundled into objects, making code modular, reusable, and easier to maintain.


πŸ”Ή What is OOP?​

  • Object-Oriented Programming (OOP) is a programming paradigm based on the concept of objects.
  • Each object can hold data (attributes) and functions (methods) that operate on that data.

Benefits of OOP in Python:

  • Reusability of code
  • Encapsulation of data
  • Modularity and abstraction
  • Easier debugging and maintenance

πŸ”Ή Classes and Objects​

A class is a blueprint for creating objects.
An object is an instance of a class.

# Defining a class
class Person:
def __init__(self, name, age):
self.name = name
self.age = age

def greet(self):
return f"Hello, my name is {self.name} and I am {self.age} years old."

# Creating objects
p1 = Person("Alice", 25)
p2 = Person("Bob", 30)

print(p1.greet()) # Output: Hello, my name is Alice and I am 25 years old.
print(p2.greet()) # Output: Hello, my name is Bob and I am 30 years old.

πŸ”Ή Attributes and Methods​

  • Attributes β†’ Variables inside a class.
  • Methods β†’ Functions defined inside a class.
class Car:
wheels = 4 # Class attribute

def __init__(self, brand, model):
self.brand = brand # Instance attribute
self.model = model

def info(self): # Instance method
return f"{self.brand} {self.model} has {Car.wheels} wheels."

c1 = Car("Tesla", "Model S")
print(c1.info()) # Tesla Model S has 4 wheels.

πŸ”Ή Encapsulation​

Encapsulation means restricting access to certain variables/methods. In Python, we use:

  • _protected_var β†’ convention for protected members
  • __private_var β†’ name mangling for private members
class BankAccount:
def __init__(self, balance):
self.__balance = balance # Private variable

def deposit(self, amount):
self.__balance += amount

def get_balance(self):
return self.__balance

acc = BankAccount(1000)
acc.deposit(500)
print(acc.get_balance()) # 1500

πŸ”Ή Inheritance​

Inheritance allows a class (child) to acquire properties of another class (parent).

# Base class
class Animal:
def speak(self):
return "This is an animal."

# Derived class
class Dog(Animal):
def speak(self):
return "Woof! Woof!"

class Cat(Animal):
def speak(self):
return "Meow!"

d = Dog()
c = Cat()
print(d.speak()) # Woof! Woof!
print(c.speak()) # Meow!

πŸ”Ή Polymorphism​

Polymorphism allows the same method name to perform different tasks depending on the object.

for animal in [Dog(), Cat()]:
print(animal.speak())

# Output:
# Woof! Woof!
# Meow!

πŸ”Ή Abstraction​

Abstraction means hiding implementation details and showing only the necessary functionality. In Python, we use the abc module to create abstract classes.

from abc import ABC, abstractmethod

class Shape(ABC):
@abstractmethod
def area(self):
pass

class Circle(Shape):
def __init__(self, radius):
self.radius = radius

def area(self):
return 3.14 * self.radius * self.radius

c = Circle(5)
print(c.area()) # 78.5

πŸ”Ή Summary​

  • Class & Object β†’ Blueprint and instance
  • Attributes & Methods β†’ Data and behavior inside a class
  • Encapsulation β†’ Restricting access
  • Inheritance β†’ Reusing parent class features
  • Polymorphism β†’ Same function name, different behavior
  • Abstraction β†’ Hiding unnecessary details