Lesson 14: Object-Oriented Programming (OOP) in Python

๐ŸŽฏ Lesson Objective

To understand the concepts of classes, objects, and OOP principles in Python, allowing you to structure programs efficiently using real-world models.


๐Ÿงฉ 1. What Is Object-Oriented Programming (OOP)?

OOP is a programming paradigm that models real-world entities as objects.
It focuses on data (attributes) and behavior (methods).

Key concepts:

  • Class: Blueprint for creating objects.
  • Object: Instance of a class.
  • Attributes: Variables in a class (properties).
  • Methods: Functions inside a class (behaviors).

โš™๏ธ 2. Creating a Class and Object

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

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

# Create object
p1 = Person("Sameer", 25)
p1.greet()

Output:

Hello, my name is Sameer and I am 25 years old.

๐Ÿ”„ 3. Class Attributes vs Instance Attributes

class Dog:
    species = "Canine"  # Class attribute

    def __init__(self, name, age):
        self.name = name  # Instance attribute
        self.age = age

d1 = Dog("Buddy", 5)
d2 = Dog("Rocky", 3)

print(d1.name, d1.species)
print(d2.name, d2.species)

Output:

Buddy Canine
Rocky Canine

๐Ÿงฐ 4. Methods and self

  • self refers to the current object instance.
  • Methods define behaviors of an object.
class Circle:
    pi = 3.14159

    def __init__(self, radius):
        self.radius = radius

    def area(self):
        return Circle.pi * self.radius ** 2

c = Circle(5)
print("Area:", c.area())

Output:

Area: 78.53975

๐Ÿงฑ 5. Inheritance

Inheritance allows a class to reuse attributes and methods of another class.

class Animal:
    def __init__(self, name):
        self.name = name

    def speak(self):
        print(f"{self.name} makes a sound")

class Dog(Animal):  # Dog inherits from Animal
    def speak(self):
        print(f"{self.name} barks")

d = Dog("Buddy")
d.speak()

Output:

Buddy barks

๐Ÿ”„ 6. Encapsulation

Encapsulation restricts direct access to attributes using private variables (__name).

class BankAccount:
    def __init__(self, balance):
        self.__balance = balance  # private attribute

    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

๐Ÿงฎ 7. Polymorphism

Polymorphism allows different classes to have methods with the same name.

class Cat:
    def speak(self):
        print("Meow")

class Dog:
    def speak(self):
        print("Bark")

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

Output:

Meow
Bark

๐ŸŒ 8. Real-Life Example โ€“ Student Class

class Student:
    def __init__(self, name, marks):
        self.name = name
        self.marks = marks

    def average(self):
        return sum(self.marks) / len(self.marks)

s1 = Student("Ali", [80, 90, 85])
s2 = Student("Sara", [75, 88, 92])

print(f"{s1.name}'s Average:", s1.average())
print(f"{s2.name}'s Average:", s2.average())

Output:

Ali's Average: 85.0
Sara's Average: 85.0


Comments

Leave a Reply

Your email address will not be published. Required fields are marked *