Day 10: Object-Oriented Programming (OOP) in Python

Day 10: Object-Oriented Programming (OOP) in Python

Topics to Cover:

  • Classes and Objects
  • Methods
  • Inheritance

Introduction to OOP

Object-Oriented Programming (OOP) is a programming paradigm based on the concept of “objects,” which can contain data and code. Data is in the form of fields (attributes or properties), and code is in the form of procedures (methods).


Classes and Objects

A class is a blueprint for creating objects. An object is an instance of a class.

Defining a Class:

class BankAccount:
    def __init__(self, account_number, balance=0):
        self.account_number = account_number
        self.balance = balance

Creating an Object:

account = BankAccount("123456789")
print(account.account_number)  # Output: 123456789
print(account.balance)  # Output: 0

Methods

Methods are functions defined inside a class. They are used to define the behaviors of an object.

Example: Adding Methods to the BankAccount Class:

class BankAccount:
    def __init__(self, account_number, balance=0):
        self.account_number = account_number
        self.balance = balance

    def deposit(self, amount):
        self.balance += amount
        return self.balance

    def withdraw(self, amount):
        if amount > self.balance:
            return "Insufficient funds"
        self.balance -= amount
        return self.balance

    def get_balance(self):
        return self.balance

Using Methods:

account = BankAccount("123456789", 100)
print(account.deposit(50))  # Output: 150
print(account.withdraw(30))  # Output: 120
print(account.get_balance())  # Output: 120

Inheritance

Inheritance allows a class to inherit attributes and methods from another class.

Example: Base Class Animal and Derived Classes Dog and Cat:

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

    def speak(self):
        pass

class Dog(Animal):
    def speak(self):
        return f"{self.name} says Woof!"

class Cat(Animal):
    def speak(self):
        return f"{self.name} says Meow!"

# Creating Objects of Derived Classes
dog = Dog("Buddy")
cat = Cat("Whiskers")

print(dog.speak())  # Output: Buddy says Woof!
print(cat.speak())  # Output: Whiskers says Meow!

Potential Problems to Solve

Problem 1: Model a Simple Bank Account

Task: Create a class to model a simple bank account.

Solution:

class BankAccount:
    def __init__(self, account_number, balance=0):
        self.account_number = account_number
        self.balance = balance

    def deposit(self, amount):
        self.balance += amount
        return self.balance

    def withdraw(self, amount):
        if amount > self.balance:
            return "Insufficient funds"
        self.balance -= amount
        return self.balance

    def get_balance(self):
        return self.balance

# Example Usage
account = BankAccount("123456789", 100)
print(account.deposit(50))  # Output: 150
print(account.withdraw(30))  # Output: 120
print(account.get_balance())  # Output: 120

Problem 2: Implement Inheritance

Task: Create a base class Animal and derived classes Dog and Cat.

Solution:

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

    def speak(self):
        pass

class Dog(Animal):
    def speak(self):
        return f"{self.name} says Woof!"

class Cat(Animal):
    def speak(self):
        return f"{self.name} says Meow!"

# Example Usage
dog = Dog("Buddy")
cat = Cat("Whiskers")

print(dog.speak())  # Output: Buddy says Woof!
print(cat.speak())  # Output: Whiskers says Meow!

Conclusion

Understanding OOP principles is fundamental to writing clean and efficient Python code. Classes and objects help in organizing code, while inheritance promotes code reuse.


Stay tuned for Day 11 of the python4ai 30-day series, where we’ll continue exploring advanced Python topics to enhance our programming skills!

Related Articles

Responses

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