What if you didn’t have to rewrite code every time you wanted something similar?
What if you could build on what already exists?
That’s exactly what inheritance does.
So far, we’ve learned how to create classes and objects, and how they help us model real-world things in code.
Now we take it one step further: inheritance.
Inheritance allows one class to use the properties and methods of another class.
In simple terms, it means: build once, reuse and extend.
🔹 The Parent Class
A parent class is the original class.
class Animal:
def speak(self):
print("Animal makes a sound")
🔹 The Child Class
A child class inherits from the parent class.
class Dog(Animal):
pass
dog1 = Dog()
dog1.speak()
Even though Dog has no method of its own, it can still use speak() from Animal.
🔹 Overriding Methods
A child class can also change how a method works.
class Dog(Animal):
def speak(self):
print("Dog barks")
dog1 = Dog()
dog1.speak()
Now the child class replaces the parent behavior.
💡 Why Inheritance Matters
Inheritance helps you:
- Reuse code instead of repeating it
- Organize programs better
- Build scalable systems
- Represent real-world relationships In real applications, inheritance is used in everything from games to banking systems. It helps code grow without becoming messy.
🌱 Final Challenge
Create:
- A parent class called
Vehiclewith a methodmove() - A child class called
Carthat inherits fromVehicle - Override the
move()method inCarto print something different Then create an object ofCarand call the method.
🎯 Final Note
If you’ve followed all the topics in this series, you now understand the foundation of programming from basic data to object-oriented thinking.
This is the point where you stop just learning syntax… and start thinking like a developer.
Top comments (0)