Inheritance Pythoonn - 063943
Inheritance Pythoonn - 063943
def speak(self):
raise NotImplementedError("Subclass must implement this
method")
class Dog(Animal):
def speak(self):
return "Woof!"
class Cat(Animal):
def speak(self):
return "Meow!"
2
class Vehicle:
def __init__(self, brand):
self.brand = brand
def drive(self):
print("Driving a", self.brand)
class Car(Vehicle):
def __init__(self, brand, model):
super().__init__(brand)
self.model = model
def drive(self):
print("Driving a", self.brand, self.model)
class ElectricCar(Car):
def __init__(self, brand, model, battery_capacity):
super().__init__(brand, model)
self.battery_capacity = battery_capacity
def charge(self):
print("Charging the", self.brand, self.model, "with a battery
capacity of", self.battery_capacity, "kWh")
class Shape:
def __init__(self, color):
self.color = color
def area(self):
pass
def perimeter(self):
pass
class Rectangle(Shape):
def __init__(self, color, length, width):
super().__init__(color)
self.length = length
self.width = width
def area(self):
return self.length * self.width
def perimeter(self):
return 2 * (self.length + self.width)
class Circle(Shape):
def __init__(self, color, radius):
super().__init__(color)
self.radius = radius
def area(self):
return 3.14 * self.radius ** 2
def perimeter(self):
return 2 * 3.14 * self.radius
class Account:
def __init__(self, account_number, balance=0):
self.account_number = account_number
self.balance = balance
def display_balance(self):
print(f"Account {self.account_number} balance: {self.balance}")
class SavingsAccount(Account):
def __init__(self, account_number, balance=0, interest_rate=0.01):
super().__init__(account_number, balance)
self.interest_rate = interest_rate
def calculate_interest(self):
interest = self.balance * self.interest_rate
self.deposit(interest)
print(f"Interest of {interest} credited to Account
{self.account_number}")
class CheckingAccount(Account):
def __init__(self, account_number, balance=0, transaction_limit=3):
super().__init__(account_number, balance)
self.transaction_limit = transaction_limit
self.transactions = 0
# Performing operations
savings_account.display_balance() # Output: Account SA001
balance: 1000
savings_account.deposit(500) # Output: Deposited 500 into
Account SA001. New balance: 1500
savings_account.calculate_interest() #
class Employee:
def __init__(self, name, salary):
self.name = name
self.salary = salary
def calculate_bonus(self):
bonus = self.salary * 0.1
return bonus
class Manager(Employee):
def __init__(self, name, salary, department):
super().__init__(name, salary)
self.department = department
def calculate_bonus(self):
bonus = self.salary * 0.15
return bonus
class Developer(Employee):
def __init__(self, name, salary, programming_language):
super().__init__(name, salary)
self.programming_language = programming_language
# Creating instances of derived classes
employee = Employee("John Doe", 50000)
manager = Manager("Jane Smith", 80000, "Marketing")
developer = Developer("Sara Johnson", 60000, "Python")
class Vehicle:
def __init__(self, make, model, year):
self.make = make
self.model = model
self.year = year
def start(self):
print("Engine started.")
def stop(self):
print("Engine stopped.")
class Car(Vehicle):
def __init__(self, make, model, year, num_doors):
super().__init__(make, model, year)
self.num_doors = num_doors
def drive(self):
print(f"{self.make} {self.model} is driving.")
class Motorcycle(Vehicle):
def __init__(self, make, model, year):
super().__init__(make, model, year)
def ride(self):
print(f"{self.make} {self.model} is riding.")
class Shape:
def __init__(self, color):
self.color = color
def get_color(self):
return self.color
def calculate_area(self):
pass
class Circle(Shape):
def __init__(self, color, radius):
super().__init__(color)
self.radius = radius
def calculate_area(self):
return 3.14 * self.radius ** 2
class Rectangle(Shape):
def __init__(self, color, length, width):
super().__init__(color)
self.length = length
self.width = width
def calculate_area(self):
return self.length * self.width
class Employee:
def __init__(self, name, emp_id):
self.name = name
self.emp_id = emp_id
def display_details(self):
print(f"Employee Name: {self.name}")
print(f"Employee ID: {self.emp_id}")
class Manager(Employee):
def __init__(self, name, emp_id, department):
super().__init__(name, emp_id)
self.department = department
def display_details(self):
super().display_details()
print(f"Department: {self.department}")
class Developer(Employee):
def __init__(self, name, emp_id, programming_language):
super().__init__(name, emp_id)
self.programming_language = programming_language
def display_details(self):
super().display_details()
print(f"Programming Language: {self.programming_language}")
print("Developer Details:")
developer.display_details()
class Shape:
def __init__(self, color):
self.color = color
def get_color(self):
return self.color
def calculate_area(self):
pass
class Circle(Shape):
def __init__(self, color, radius):
super().__init__(color)
self.radius = radius
def calculate_area(self):
return 3.14 * self.radius ** 2
class Rectangle(Shape):
def __init__(self, color, length, width):
super().__init__(color)
self.length = length
self.width = width
def calculate_area(self):
return self.length * self.width
10