Lesson Plan: Object-Oriented Programming (OOP) in Python
Objective:
By the end of this lesson, students will:
● Understand the core principles of Object-Oriented Programming (OOP).
● Learn how to create and use classes and objects.
● Apply OOP concepts in a real-world project.
1. What is Object-Oriented Programming (OOP)?
OOP is a programming paradigm that uses objects and classes to structure code in a more
organized way.
Benefits of OOP:
✅ Reusability (Write once, use multiple times)
✅ Easy to maintain and modify code
✅ Organizes complex programs into simpler components
2. Key OOP Concepts
1️⃣ Classes and Objects
● Class: A blueprint for creating objects.
● Object: An instance of a class.
Example:
class Animal:
def __init__(self, name, sound):
self.name = name
self.sound = sound
def make_sound(self):
print(self.name + " says " + self.sound)
cat = Animal("Cat", "Meow")
dog = Animal("Dog", "Woof")
cat.make_sound()
dog.make_sound()
2️⃣ Encapsulation
Encapsulation is the practice of keeping data safe by making variables private.
Example:
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()) # Output: 1500
3️⃣ Inheritance
Inheritance allows a class to inherit attributes and methods from another class.
Example:
class Vehicle:
def __init__(self, brand):
self.brand = brand
def honk(self):
print("Honk! Honk!")
class Car(Vehicle):
def __init__(self, brand, model):
super().__init__(brand)
self.model = model
car = Car("Toyota", "Corolla")
print(car.brand, car.model)
car.honk()
4️⃣ Polymorphism
Polymorphism allows different classes to use the same method name with different
implementations.
Example:
class Bird:
def sound(self):
print("Chirp Chirp")
class Dog:
def sound(self):
print("Bark Bark")
def make_sound(animal):
animal.sound()
bird = Bird()
dog = Dog()
make_sound(bird)
make_sound(dog)
Project: Simple Library Management System
Students will build a program to manage a small library using OOP concepts.
Features:
● Add new books
● Borrow books
● Return books
Code:
class Book:
def __init__(self, title, author):
self.title = title
self.author = author
self.available = True
def borrow(self):
if self.available:
self.available = False
print(f"You borrowed '{self.title}'")
else:
print(f"Sorry, '{self.title}' is not available.")
def return_book(self):
self.available = True
print(f"You returned '{self.title}'")
class Library:
def __init__(self):
self.books = []
def add_book(self, book):
self.books.append(book)
print(f"Book '{book.title}' added to the library.")
def show_books(self):
print("Available books:")
for book in self.books:
status = "Available" if book.available else "Borrowed"
print(f"{book.title} by {book.author} - {status}")
# Example usage
lib = Library()
book1 = Book("Harry Potter", "J.K. Rowling")
book2 = Book("The Hobbit", "J.R.R. Tolkien")
lib.add_book(book1)
lib.add_book(book2)
lib.show_books()
book1.borrow()
lib.show_books()
book1.return_book()
lib.show_books()
Learning Outcomes:
✅ Using classes and objects to structure programs
✅ Implementing encapsulation to protect data
✅ Using inheritance to reuse code
✅ Applying polymorphism for flexibility
✅ Building a real-world Library Management System
Recap:
● Classes & Objects help create reusable components.
● Encapsulation secures data.
● Inheritance enables code reusability.
● Polymorphism allows flexibility.
● Project application strengthens OOP concepts!
This lesson makes learning OOP fun and practical with an engaging project!