Implementing Inheritance and Polymorphism with Python Classes.
Course Title: Modern Python Programming: Best Practices and Trends Section Title: Object-Oriented Programming (OOP) in Python Topic: Implement a class-based system with inheritance and polymorphism.(Lab topic)
Objective: In this lab, we will implement a class-based system that uses inheritance and polymorphism to demonstrate the power of Object-Oriented Programming (OOP) in Python. By the end of this exercise, you will have a solid understanding of how to design and implement a hierarchy of classes that inherit behavior from parent classes and override behavior using polymorphism.
Step 1: Define the Base Class
The base class is the foundation of our class-based system. It defines the common attributes and methods that are shared by all classes in the hierarchy. For this lab, let's create a simple base class called Vehicle
:
# base_class.py
class Vehicle:
def __init__(self, make, model, year):
self.make = make
self.model = model
self.year = year
def description(self):
return f"{self.year} {self.make} {self.model}"
Step 2: Define Derived Classes
Derived classes inherit from the base class and add their own specific attributes and methods. For this lab, let's create two derived classes: Car
and Truck
. These classes will inherit from the Vehicle
base class:
# derived_classes.py
from base_class import Vehicle
class Car(Vehicle):
def __init__(self, make, model, year, num_doors):
super().__init__(make, model, year)
self.num_doors = num_doors
def description(self):
return f"{super().description()} with {self.num_doors} doors"
class Truck(Vehicle):
def __init__(self, make, model, year, bed_size):
super().__init__(make, model, year)
self.bed_size = bed_size
def description(self):
return f"{super().description()} with a {self.bed_size} bed"
Step 3: Implement Polymorphism
Polymorphism is the ability of an object to take on multiple forms. In Python, we can achieve polymorphism using method overriding or method overloading. For this lab, let's use method overriding to demonstrate polymorphism. We will create a Vehicle
method called drive
that is overridden by the Car
and Truck
classes:
# base_class.py
class Vehicle:
...
def drive(self):
return "The vehicle is moving"
# derived_classes.py
class Car(Vehicle):
...
def drive(self):
return "The car is driving"
class Truck(Vehicle):
...
def drive(self):
return "The truck is hauling"
Step 4: Test the System Now that we have implemented the class-based system with inheritance and polymorphism, let's test it:
# test_system.py
from derived_classes import Car, Truck
car = Car("Toyota", "Corolla", 2020, 4)
truck = Truck("Ford", "F-150", 2019, "6.5 ft")
print(car.description())
print(car.drive())
print(truck.description())
print(truck.drive())
This code will output:
2020 Toyota Corolla with 4 doors
The car is driving
2019 Ford F-150 with a 6.5 ft bed
The truck is hauling
Conclusion: In this lab, we have successfully implemented a class-based system with inheritance and polymorphism. We have demonstrated how to define a base class and derived classes, and how to use method overriding to achieve polymorphism. These concepts are fundamental to Object-Oriented Programming (OOP) in Python.
Further Reading:
What's Next: In the next topic, we will learn about reading and writing files (text, CSV, JSON) with Python. This will include file handling, reading, and writing data to files. If you have any questions or need help with this topic, feel free to leave a comment or ask for help.
Images

Comments