Inheritance

noun

say it: “in-HAIR-ih-tuns” /ɪnˈhɛrɪtəns/

Making a new class that starts with everything another class has, then adds or changes things, such as a Truck class built on Vehicle.

What is inheritance?

Inheritance lets a new class start with everything another class has and then add or change things.

class Vehicle:
    def start(self):
        print("Engine on")

class Truck(Vehicle):          # Truck gets start() for free
    def haul(self):
        print("Hauling cargo")

Truck().start()  # Engine on

In a sentence

  • “Through inheritance, Truck gets start() from Vehicle.”
  • “Inheritance let the custom error class start from Exception.”

Level: Journeyman · More Python terms at this level

Updated

In other subjects