Showing posts with label inheritance in Python. Show all posts
Showing posts with label inheritance in Python. Show all posts

Monday, March 16, 2026

Lesson 9: Object-Oriented Programming (OOP) in Python | Coding Class Series

March 16, 2026 0



Lesson 9: Object-Oriented Programming (OOP) in Python | Coding Class Series

Introduction

Welcome to Lesson 9!
In this lesson, we will learn Object-Oriented Programming (OOP) in Python, which is a powerful way to structure code using classes and objects. OOP helps you write reusable, organized, and maintainable programs.


1. What is OOP?

Object-Oriented Programming (OOP) is a programming paradigm that uses objects to represent real-world entities.
Key concepts:

  • Class → Blueprint of an object
  • Object → Instance of a class
  • Attributes → Properties of an object
  • Methods → Functions of an object

2. Creating a Class and Object

Example:

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

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

# Creating an object
person1 = Person("Alice", 20)
person1.greet()  # Output: Hello, my name is Alice and I am 20 years old.

Explanation:

  • __init__ → Constructor method, runs automatically when object is created
  • self → Refers to the current object

3. Inheritance

Inheritance allows a class to reuse code from another class.

class Student(Person):
    def __init__(self, name, age, student_id):
        super().__init__(name, age)
        self.student_id = student_id

    def show_id(self):
        print(f"My student ID is {self.student_id}")

student1 = Student("Bob", 18, "S123")
student1.greet()      # Inherited method
student1.show_id()    # Own method

Benefits:

  • Reuse existing code
  • Create hierarchical relationships
  • Simplify complex programs

4. Encapsulation

Encapsulation is the concept of hiding private details of an object.

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

account = BankAccount(1000)
account.deposit(500)
print(account.get_balance())  # Output: 1500

Note: Prefix __ to make an attribute private.


5. Polymorphism

Polymorphism allows methods with the same name to behave differently depending on the object.

class Dog:
    def speak(self):
        print("Woof!")

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

animals = [Dog(), Cat()]
for animal in animals:
    animal.speak()
# Output: Woof! Meow!

6. Practice Exercises

  1. Create a class Car with attributes brand and year, and method display_info().
  2. Create a subclass ElectricCar that inherits from Car and adds battery_capacity.
  3. Create objects of both classes and call their methods.
  4. Make a class Account with private balance and methods deposit and withdraw.