Object- Oriented Programming (OOP) in
Python
Topic: Implement Encapsulation, Inheritance, and
Polymorphism
Objective:
To understand and implement the core OOP principles — Encapsulation, Inheritance,
and Polymorphism — using Python classes and objects.
1. Encapsulation
Encapsulation is the concept of bundling data (attributes) and methods (functions)
that operate on that data into a single unit — a class.
It also restricts direct access to some data using private or protected attributes.
Example: Encapsulation
class Account:
def __init__(self, owner, balance):
[Link] = owner # Public attribute
self.__balance = balance # Private attribute
def deposit(self, amount):
self.__balance += amount
print(f"Deposited ₹{amount}. New Balance: ₹{self.__balance}")
def withdraw(self, amount):
if amount <= self.__balance:
self.__balance - = amount
print(f"Withdrawn ₹{amount}. Remaining Balance: ₹{self.__balance}")
else:
print("Insufficient Balance!")
def show_balance(self):
print(f"Account Holder: {[Link]}, Balance: ₹{self.__balance}")
# Creating object
acc = Account("Aditya", 5000)
[Link](1500)
[Link] (2000)
acc.show_balance()
# Trying to access private variable directly (will cause error)
# print(acc.__balance)
Output:
Deposited ₹1500. New Balance: ₹6500
Withdrawn ₹2000. Remaining Balance: ₹4500
Account Holder: Aditya, Balance: ₹4500
2. Inheritance
Inheritance allows one class (child) to acquire the properties and methods of another
class (parent).
It promotes code reusability and establishes a relationship between classes.
Example: Inheritance
# Parent Class
class Animal:
def __init__(self, name):
[Link] = name
def speak(self):
print(f"{[Link]} makes a sound.")
# Child Class
class Dog(Animal):
def speak(self): # Method overriding
print(f"{[Link]} barks!")
# Another Child Class
class Cat(Animal):
def speak(self):
print(f"{[Link]} meows!")
# Creating objects
dog1 = Dog("Buddy")
cat1 = Cat("Whiskers")
[Link]()
[Link]()
Output:
Buddy barks!
Whiskers meows!
3. Polymorphism
Polymorphism means one function or method behaves differently based on the
object that calls it.
It allows for method overriding and fl exibility in code.
Example: Polymorphism with Method Overriding
class Bird:
def intro(self):
print("There are many types of birds.")
def fl ight(self):
print("Some birds can fly, some cannot.")
class Sparrow(Bird):
def fl ight(self):
print("Sparrows can fly high in the sky.")
class Penguin(Bird):
def fl ight(self):
print("Penguins cannot fly, they swim instead.")
obj_bird = Bird()
obj_sparrow = Sparrow()
obj_penguin = Penguin()
# Demonstrating Polymorphism
for bird in (obj_bird, obj_sparrow, obj_penguin):
[Link]()
bird.fl ight()
Output:
There are many types of birds.
Some birds can fly, some cannot.
There are many types of birds.
Sparrows can fly high in the sky.
There are many types of birds.
Penguins cannot fly, they swim instead.
Example: Polymorphism with Functions
class Car:
def mileage(self):
print("Mileage is 20 km/ l")
class Bike:
def mileage(self):
print("Mileage is 60 km/ l")
def show_mileage(vehicle):
[Link]()
c = Car()
b = Bike()
show_mileage(c)
show_mileage(b)
Output:
Mileage is 20 km/ l
Mileage is 60 km/ l
Conclusion:
Encapsulation Protects data and hides internal details.
Inheritance Reuses and extends existing code.
Polymorphism Provides fl exibility by allowing one interface for multiple
object types.
Together, these principles make Python’s OOP powerful, organized, and reusable for
real- world applications.
Top of Form
Bottom of Form