Python OOP (Object-Oriented Programming) — With Real Examples
Python OOP (Object-Oriented Programming ) — With Real Examples
Object-Orientation Programming (OOP) is one of the most important concepts in Python especially if you want to build scalable and real-world applications.
What is OOP?
OOP is a programming approach where you organize code using objects and classes instead of just functions.
Think of it like:
- Class = Blueprint
- Object = Real-world item built from that blueprint
Class (Definition)
A class is a blueprint or template used to create objects. It defines properties (variables) and behaviors (methods) that the objects will have. Suppose Student as a class which has properties as his name and marks.
Example:
class Student:
name ="Rahul"
marks = 90
Object (Definition)
A object is an instance of a class. It represents a real-world entity and can access the properties and method defined in the class.
Example:
s1= Student()
print(s1.name)
4 Pillars of OOP in Python
- Encapsulation
- Abstraction
- Inheritance
- Polymorphism
Let's understand each with real examples
Encapsulation (Data Hiding)
Encapsulation is the concept of wrapping data(variables) and methods together into a single class. It means restricting direct access to data and controlling it through methods.
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
account = BankAccount(1000)
account.deposit(500)
print(account.get_balance()) #1500
Encapsulation is used for protecting data from accidental changes, which help in improve code security and makes code more organized and maintainable. We can see this in above example where, _balance is private and cannot be accessed directly.
Abstraction (Hiding Complexity)
Abstraction means showing only essential details and hiding internal logic, which contributes to reduces complexity, improves code readability and makes systems easier to use.
Example:
from abc import ABC, abstractmethod
class Vehicle(ABC):
@abstractmethod
def start(self):
pass
class Car(Vechicle):
def start(self):
print("Car starts with key")
car = Car()
car.start()
User only knows what happens, not how it works internally.
Inheritance(Code Reusability)
Inheritance allows one class to reuse properties of another class. It is used to avoid code duplication, improve code reusability, makes code easier to maintain and supports hierarchical relationships.
Example:
class Animal:
def speak(self):
print("Animal makes a sound")
class Dog(Animal):
def bark(self):
print("Dog barks")
d = Dog()
d.speak()
d.bark()
Dog inherits from Animal.
Polymorphism (Many Forms)
Same method behaves differently for different objects.
Example:
class Bird:
def sound(self):
print("Bird chirps")
class Cat:
def sound(self):
print("Cat meows")
for animal in [Bird(), Cat()]:
animal.sound()
Same function sound() is different behavior.
Real-World Example: Student Management System
class Student:
def __init__(self, name, marks):
self.name = name
self.marks = marks
def display(self):
print(f"Name: {self.name}, Marks: {self.marks}")
def is_pass(self):
return self.marks >=40
s1 = Student("Astha", 85)
s1.diplay()
if s1.is_pass():
print("Result: Pass")
else:
print("Result: Fail")
This is how OOP is used in real applications.
Practice Questions
- Bank System
- Library management system

Comments
Post a Comment