Python Mastery – From Basics to Brilliance / Object-Oriented Programming (OOP)

Inheritance

Inheritance allows one class to reuse the properties and methods of another class.

  • Parent class (Base): The class being inherited from.

  • Child class (Derived): The class that inherits.


💡 Example:

class Animal:
    def speak(self):
        print("Some sound")

class Dog(Animal):
    def bark(self):
        print("Woof")

pet = Dog()
pet.speak()  # Inherited from Animal
pet.bark()   # Defined in Dog
Some sound
Woof

🔍 Why Use Inheritance?

✅ Promotes code reuse

✅ Supports hierarchical organization

✅ Helps in extending functionality without rewriting


🌐 Real-life Analogy:

A child inherits traits from a parent – like eye color or height.

Similarly, a class can inherit behavior and properties from another class.


🧪 Bonus Example:

class Vehicle:
    def start(self):
        print("Vehicle started")

class Car(Vehicle):
    def drive(self):
        print("Car is driving")

tesla = Car()
tesla.start()   # from Vehicle
tesla.drive()   # from Car