0
0
Pythonprogramming~20 mins

Parent and child classes in Python - Mini Project: Build & Apply

Choose your learning style9 modes available
Parent and Child Classes
📖 Scenario: Imagine you are creating a simple program to manage vehicles. You want to have a general vehicle type and a specific type for cars.
🎯 Goal: You will create a parent class called Vehicle and a child class called Car that inherits from Vehicle. You will then create a car object and display its details.
📋 What You'll Learn
Create a parent class named Vehicle with an __init__ method that sets make and model attributes.
Create a child class named Car that inherits from Vehicle and adds an attribute year.
Create an instance of Car with specific values for make, model, and year.
Print the car's details using the attributes.
💡 Why This Matters
🌍 Real World
Understanding parent and child classes helps organize code for things like vehicles, animals, or employees where many share common traits but also have unique features.
💼 Career
Inheritance is a key concept in many programming jobs to write clean, reusable, and organized code.
Progress0 / 4 steps
1
Create the parent class Vehicle
Create a parent class called Vehicle with an __init__ method that takes make and model as parameters and sets them as attributes.
Python
Need a hint?

Use class Vehicle: to start the class. Inside, define def __init__(self, make, model): and set self.make = make and self.model = model.

2
Create the child class Car
Create a child class called Car that inherits from Vehicle. Add an __init__ method that takes make, model, and year as parameters. Call the parent __init__ method for make and model, and set self.year = year.
Python
Need a hint?

Use class Car(Vehicle): to inherit. Inside __init__, call super().__init__(make, model) and then set self.year = year.

3
Create a Car object
Create an object called my_car from the Car class with make as 'Toyota', model as 'Corolla', and year as 2020.
Python
Need a hint?

Create the object by calling Car('Toyota', 'Corolla', 2020) and assign it to my_car.

4
Print the car details
Print the details of my_car in this exact format: "Toyota Corolla 2020" using the attributes make, model, and year.
Python
Need a hint?

Use print(f"{my_car.make} {my_car.model} {my_car.year}") to display the details.