Inheriting attributes and methods
📖 Scenario: Imagine you are creating a simple program to manage different types of vehicles. All vehicles share some common features like having a brand and a method to describe themselves. But specific types of vehicles, like cars, have extra features.
🎯 Goal: You will build a base class called Vehicle with common attributes and methods. Then, you will create a subclass called Car that inherits from Vehicle and adds its own attribute. Finally, you will create an object of Car and print its description.
📋 What You'll Learn
Create a class called
Vehicle with an __init__ method that takes brand as a parameter and stores it.Add a method called
describe in Vehicle that returns a string describing the vehicle brand.Create a subclass called
Car that inherits from Vehicle.Add an
__init__ method to Car that takes brand and model, calls the parent __init__, and stores model.Override the
describe method in Car to include both brand and model in the description.Create an object of
Car with brand "Toyota" and model "Corolla".Print the description of the car object.
💡 Why This Matters
🌍 Real World
Inheritance helps organize code when many objects share common features but also have unique parts. For example, different types of vehicles share some traits but also have their own details.
💼 Career
Understanding inheritance is important for writing clean, reusable code in many programming jobs, especially in software development and object-oriented programming.
Progress0 / 4 steps