Inheritance is a fundamental concept in object-oriented programming, which allows a class to inherit properties and methods from another class. There are several types of inheritance, including:
Scenario: Shapes are a basic concept in geometry, and different types of shapes have different attributes and methods. For example, a circle has a radius and a method to calculate its area, while a square has a side length and a method to calculate its perimeter.
Exercise: Create a base class called "Shape" with methods to calculate the area and perimeter of a shape. Create derived classes for each type of shape with additional attributes and methods specific to their shape. Create instances of each type of shape and test their specific methods.
import math
class Shape:
def area(self):
pass
def perimeter(self):
pass
class Circle(Shape):
def __init__(self, radius):
self.radius = radius
def area(self):
return math.pi * self.radius ** 2
def perimeter(self):
return 2 * math.pi * self.radius
class Square(Shape):
def __init__(self, side_length):
self.side_length = side_length
def area(self):
return self.side_length ** 2
def perimeter(self):
return 4 * self.side_length
class Rectangle(Shape):
def __init__(self, l, w):
self.length = l
self.width = w
def area(self):
return self.length * self.width
def perimeter(self):
return 2 * (self.length + self.width)
# Create instances of each type of shape
circle = Circle(5)
square = Square(10)
rectangle = Rectangle(6, 8)
# Test specific methods for each type of shape
print(circle.area()) # Output: 78.53981633974483
print(square.perimeter()) # Output: 40
print(rectangle.area()) # Output: 48.0
print(rectangle.perimeter()) # Output: 28
********************************************************************************
Scenario: A company has multiple types of vehicles such as cars, trucks, and motorcycles. All vehicles have a make, model, year, and color. However, each type of vehicle has additional attributes and functions specific to their type. For example, cars have a number of doors, trucks have a towing capacity, and motorcycles have a top speed.
Exercise: Create a base class called "Vehicle" with attributes for make, model, year, and color. Create derived classes for each type of vehicle with additional attributes and functions specific to their type. Create instances of each type of vehicle and test their specific functions.
class Vehicle:
def __init__(self, make, model, year, color):
self.make = make
self.model = model
self.year = year
self.color = color
class Car(Vehicle):
def __init__(self, make, model, year, color, num_doors):
super().__init__(make, model, year, color)
self.num_doors = num_doors
def honk(self):
print("Beep beep!")
class Truck(Vehicle):
def __init__(self, make, model, year, color, towing_capacity):
super().__init__(make, model, year, color)
self.towing_capacity = towing_capacity
def tow(self):
print(f"Towing with {self.towing_capacity} lbs capacity.")
class Motorcycle(Vehicle):
def __init__(self, make, model, year, color, top_speed):
super().__init__(make, model, year, color)
self.top_speed = top_speed
def wheelie(self):
print("Popping a wheelie!")
# Create instances of each type of vehicle
my_car = Car("Toyota", "Corolla", 2021, "red", 4)
my_truck = Truck("Ford", "F-150", 2022, "blue", 8000)
my_motorcycle = Motorcycle("Honda", "CBR600RR", 2020, "black", 160)
# Test specific functions for each type of vehicle
my_car.honk() # Output: "Beep beep!"
my_truck.tow() # Output: "Towing with 8000 lbs capacity."
my_motorcycle.wheelie() # Output: "Popping a wheelie!"
********************************************************************************
Scenario: A company has multiple types of employees such as developers, designers, and managers. All employees have a name, age, and salary. However, each type of employee has additional attributes and functions specific to their role. For example, developers have a programming language they specialize in, designers have a portfolio of work they've created, and managers have a team they manage.
Exercise: Create a base class called "Employee" with attributes for name, age, and salary. Create derived classes for each type of employee with additional attributes and functions specific to their role. Create instances of each type of employee and test their specific functions.
class Employee:
def __init__(self, name, age, salary):
self.name = name
self.age = age
self.salary = salary
def get_info(self):
return f"Name: {self.name}, Age: {self.age}, Salary: {self.salary}"
class Developer(Employee):
def __init__(self, name, age, salary, programming_language):
super().__init__(name, age, salary)
self.programming_language = programming_language
def get_info(self):
return f"{super().get_info()}, Programming Language: {self.programming_language}"
class Designer(Employee):
def __init__(self, name, age, salary, portfolio):
super().__init__(name, age, salary)
self.portfolio = portfolio
def get_info(self):
return f"{super().get_info()}, Portfolio: {self.portfolio}"
class Manager(Employee):
def __init__(self, name, age, salary, team):
super().__init__(name, age, salary)
self.team = team
def get_info(self):
return f"{super().get_info()}, Team: {self.team}"
# Create instances of each type of Employee
dev = Developer("Megha", 30, 80000, "Python")
print(dev.get_info()) # Name: Megha, Age: 40, Salary: 150000, Programming Language: Python
designer = Designer("Jei", 28, 75000, "https://jmndesigns.com")
print(designer.get_info()) # Name: Jei, Age: 28, Salary: 75000, Portfolio: https://jmndesigns.com
manager = Manager("Navan", 45, 100000, "Marketing")
print(manager.get_info()) # Name: Navan, Age: 45, Salary: 200000, Team: Marketing
********************************************************************************
Scenario: A person has basic attributes such as name, age, and gender. However, there are different types of people with additional attributes and functions specific to their type. For example, an employee has a salary, a manager has a department, and a customer has a purchase history.
Exercise: Create a base class called "Person" with attributes for name, age, and gender. Create derived classes for each type of person with additional attributes and functions specific to their type. Create instances of each type of person and test their specific functions.
class Person:
def __init__(self, name, age, gender):
self.name = name
self.age = age
self.gender = gender
class Employee(Person):
def __init__(self, name, age, gender, salary):
super().__init__(name, age, gender)
self.salary = salary
def get_salary(self):
return self.salary
class Manager(Person):
def __init__(self, name, age, gender, department):
super().__init__(name, age, gender)
self.department = department
def get_department(self):
return self.department
class Customer(Person):
def __init__(self, name, age, gender, purchase_history):
super().__init__(name, age, gender)
self.purchase_history = purchase_history
def get_purchase_history(self):
return self.purchase_history
# Create instances of each type of person
employee = Employee("Megha", 40, "male", 50000)
manager = Manager("Jei", 35, "female", "Sales")
customer = Customer("Navan", 20, "female", ["book", "pen", "shirt"])
# Test specific functions for each type of person
print(employee.get_salary()) # Output: 50000
print(manager.get_department()) # Output: "Sales"
print(customer.get_purchase_history()) # Output: ["book", "pen", "shirt"]
Write a Python program to demonstrate MRO in multiple inheritance. Create a class A with a method foo() that prints "A foo", and create two subclasses B and C that both inherit from A. B should override foo() to print "B foo", and C should override foo() to print "C foo". Finally, create a subclass D that inherits from both B and C, and call foo() on an instance of D. Verify that the method resolution order follows the MRO.
Comments
Post a Comment