Skip to main content

Inheritance

Passing Down the Hacker’s Legacy

Imagine you’re a hacker clan leader. You’ve built a powerful toolkit, and now your apprentices want to use it. Instead of rewriting everything, you pass down your tools. They inherit your arsenal but can also add their own upgrades.

This is the essence of inheritance in OOP: creating new classes that reuse and extend existing ones. It’s about efficiency, reusability, and building hierarchies where specialized classes evolve from general ones.


Why Inheritance Matters

  • Reusability: Avoid rewriting code as child classes inherit attributes and methods from parent classes.
  • Extensibility: Child classes can add new features or override existing ones.
  • Hierarchy: Models real‑world relationships (e.g. a “Car” is a type of “Vehicle”).
  • Polymorphism: Child classes can redefine behavior while keeping the same interface.
  • Hacker’s Advantage: Inheritance lets you build scalable systems where base logic is shared, and specialized logic is layered on top.

Basic Inheritance

class Vehicle:
    def __init__(self, brand):
        self.brand = brand

    def drive(self):
        print(f"{self.brand} is moving.")

class Car(Vehicle):  # Car inherits from Vehicle
    def honk(self):
        print("Beep beep!")
my_car = Car("Tesla")
my_car.drive()   # inherited method
my_car.honk()    # new method
  • Why? Car inherits drive() from Vehicle but adds its own behavior.

Overriding Methods

class Vehicle:
    def drive(self):
        print("Vehicle is moving.")

class Bike(Vehicle):
    def drive(self):
        print("Bike is speeding away!")
bike = Bike()
bike.drive()  # Output: Bike is speeding away!
  • Why? Child classes can override parent methods to customize behavior.

Using super()

class Vehicle:
    def __init__(self, brand):
        self.brand = brand

class Truck(Vehicle):
    def __init__(self, brand, capacity):
        super().__init__(brand)   # call parent constructor
        self.capacity = capacity

truck = Truck("Volvo", 5000)
print(truck.brand, truck.capacity)
  • Why? super() allows child classes to reuse parent initialization logic while adding their own.

Real‑World Example

class Employee:
    def __init__(self, name):
        self.name = name

    def work(self):
        print(f"{self.name} is working.")

class Manager(Employee):
    def work(self):
        print(f"{self.name} is managing the team.")

class Developer(Employee):
    def work(self):
        print(f"{self.name} is writing code.")
emp1 = Manager("Shubham")
emp2 = Developer("Aditi")

emp1.work()  # Shubham is managing the team.
emp2.work()  # Aditi is writing code.
  • Why? Inheritance models organizational hierarchies - different roles share a base identity but specialize their behavior.

The Hacker’s Notebook

  • Inheritance lets child classes reuse parent attributes and methods. Overriding allows customization of inherited behavior.
  • super() enables extending parent logic without rewriting it. Hierarchies model real‑world relationships, making systems scalable and intuitive.

Hacker’s Mindset: treat inheritance as passing down your toolkit. Build strong foundations, then extend them with specialized hacks.


Tips, Tricks, Roadmaps, Resources, Networking, Motivation, Guidance, and Cool Stuff ♥

Updated on Jan 2, 2026