0
0
Pythonprogramming~30 mins

Abstract base classes overview in Python - Mini Project: Build & Apply

Choose your learning style9 modes available
Abstract base classes overview
📖 Scenario: Imagine you are designing a simple system for different types of vehicles. Each vehicle must have a method to start the engine, but how it starts can be different for each vehicle type.
🎯 Goal: You will create an abstract base class called Vehicle with an abstract method start_engine. Then, you will create two classes, Car and Motorcycle, that inherit from Vehicle and implement the start_engine method differently. Finally, you will create instances of these classes and call their start_engine methods to see the output.
📋 What You'll Learn
Create an abstract base class called Vehicle using the abc module
Define an abstract method start_engine inside Vehicle
Create a class Car that inherits from Vehicle and implements start_engine with a print statement
Create a class Motorcycle that inherits from Vehicle and implements start_engine with a different print statement
Create instances of Car and Motorcycle and call their start_engine methods
Print the outputs of calling start_engine on both instances
💡 Why This Matters
🌍 Real World
Abstract base classes help define common interfaces for different types of objects, ensuring they all have certain methods. This is useful in designing systems where different objects share behavior but implement it differently.
💼 Career
Understanding abstract base classes is important for writing clean, maintainable code in many software development jobs, especially when working with frameworks or large codebases that rely on polymorphism.
Progress0 / 4 steps
1
Create the abstract base class
Import the ABC and abstractmethod from the abc module. Then create an abstract base class called Vehicle that inherits from ABC. Inside Vehicle, define an abstract method called start_engine using the @abstractmethod decorator.
Python
Need a hint?

Use from abc import ABC, abstractmethod to import the tools for abstract classes. Then create class Vehicle(ABC):. Use @abstractmethod above start_engine to mark it as abstract.

2
Create the Car class
Create a class called Car that inherits from Vehicle. Inside Car, implement the start_engine method to print the exact text "Car engine started with a key.".
Python
Need a hint?

Define class Car(Vehicle):. Inside it, write def start_engine(self): and use print("Car engine started with a key.").

3
Create the Motorcycle class
Create a class called Motorcycle that inherits from Vehicle. Inside Motorcycle, implement the start_engine method to print the exact text "Motorcycle engine started with a button.".
Python
Need a hint?

Define class Motorcycle(Vehicle):. Inside it, write def start_engine(self): and use print("Motorcycle engine started with a button.").

4
Create instances and call start_engine
Create an instance called my_car of the Car class and an instance called my_motorcycle of the Motorcycle class. Then call the start_engine method on both instances. Print the outputs exactly as they appear.
Python
Need a hint?

Create my_car = Car() and my_motorcycle = Motorcycle(). Then call my_car.start_engine() and my_motorcycle.start_engine().