Certainly! Here’s a write-up on object-oriented programming (OOP) concepts in Python with examples from the retail and banking domains, along with Python scripts to illustrate each concept.

1. Class and Object:

A class is a blueprint for creating objects. It defines the attributes and methods that an object of that class will have. An object is an instance of a class.

Retail Example:

class Product:
def __init__(self, name, price, quantity):
self.name = name
self.price = price
self.quantity = quantity

def display_info(self):
print(f”Product: {self.name}, Price: {self.price}, Quantity: {self.quantity}”)

# Creating objects of the Product class
product1 = Product(“Shirt”, 19.99, 50)
product2 = Product(“Jeans”, 39.99, 30)

# Accessing object attributes and calling methods
product1.display_info()
product2.display_info()

Banking Example:

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

def display_balance(self):
print(f”Account Number: {self.account_number}, Balance: {self.balance}”)

# Creating objects of the BankAccount class
account1 = BankAccount(“1234567890”, 5000.0)
account2 = BankAccount(“9876543210”, 10000.0)

# Accessing object attributes and calling methods
account1.display_balance()
account2.display_balance()

2. Inheritance:

Inheritance allows a class to inherit attributes and methods from another class. It promotes code reuse and enables the creation of specialized classes based on existing classes.

Retail Example:

class Clothing(Product):
def __init__(self, name, price, quantity, size):
super().__init__(name, price, quantity)
self.size = size

def display_info(self):
super().display_info()
print(f”Size: {self.size}”)

# Creating an object of the Clothing class
clothing1 = Clothing(“T-Shirt”, 14.99, 100, “Large”)

# Calling the overridden display_info() method
clothing1.display_info()

Banking Example:

class SavingsAccount(BankAccount):
def __init__(self, account_number, balance, interest_rate):
super().__init__(account_number, balance)
self.interest_rate = interest_rate

def calculate_interest(self):
return self.balance * self.interest_rate

# Creating an object of the SavingsAccount class
savings_account1 = SavingsAccount(“1234567890″, 5000.0, 0.03)

# Calling the inherited display_balance() method
savings_account1.display_balance()

# Calling the calculate_interest() method
interest = savings_account1.calculate_interest()
print(f”Interest: {interest}”)

3. Polymorphism:

Polymorphism allows objects of different classes to be treated as objects of a common base class. It enables the use of a single interface to represent different types of objects.

Retail Example:

class InventoryItem:
def __init__(self, name, price, quantity):
self.name = name
self.price = price
self.quantity = quantity

def calculate_total_price(self):
return self.price * self.quantity

class Book(InventoryItem):
def __init__(self, name, price, quantity, author):
super().__init__(name, price, quantity)
self.author = author

class Electronics(InventoryItem):
def __init__(self, name, price, quantity, warranty):
super().__init__(name, price, quantity)
self.warranty = warranty

# Creating objects of different classes
book = Book(“Python Programming”, 29.99, 20, “John Doe”)
electronics = Electronics(“Smartphone”, 499.99, 10, “1 Year”)

# Treating objects of different classes as objects of the base class
inventory_items = [book, electronics]
for item in inventory_items:
print(f”Total Price: {item.calculate_total_price()}”)

Banking Example:

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

def deposit(self, amount):
self.balance += amount
print(f”Deposited {amount}. New balance: {self.balance}”)

class CheckingAccount(BankAccount):
def __init__(self, account_number, balance, overdraft_limit):
super().__init__(account_number, balance)
self.overdraft_limit = overdraft_limit

def withdraw(self, amount):
if self.balance – amount >= -self.overdraft_limit:
self.balance -= amount
print(f”Withdrawn {amount}. New balance: {self.balance}”)
else:
print(“Insufficient funds.”)

class SavingsAccount(BankAccount):
def __init__(self, account_number, balance, interest_rate):
super().__init__(account_number, balance)
self.interest_rate = interest_rate

def calculate_interest(self):
interest = self.balance * self.interest_rate
self.balance += interest
print(f”Interest calculated. New balance: {self.balance}”)

# Creating objects of different classes
checking_account = CheckingAccount(“1234567890”, 1000.0, 500.0)
savings_account = SavingsAccount(“9876543210”, 5000.0, 0.03)

# Treating objects of different classes as objects of the base class
accounts = [checking_account, savings_account]
for account in accounts:
account.deposit(500.0)

These examples demonstrate the key concepts of object-oriented programming in Python, applied to the retail and banking domains. The examples show how classes and objects can be created, how inheritance can be used to create specialized classes, and how polymorphism allows objects of different classes to be treated as objects of a common base class.

Leave a Reply

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

DeepNeuron