Inheritance lets one class reuse the attributes and methods of another, instead of rewriting the same code over and over. The class being reused is the parent (or base/super) class, and the class that reuses it is the child (or derived/sub) class. It's one of the core ideas in object-oriented Python, and it's what lets you model "a Dog is an Animal" or "a SavingsAccount is an Account" relationships cleanly.
If you haven't yet, it helps to be comfortable with classes and objects and the __init__ constructor before this one, since inheritance builds directly on both.
Basic Inheritance Syntax
To inherit from a class, put the parent class's name in parentheses after the child class's name. The child automatically gets every method and attribute the parent defines.
class Animal:
def __init__(self, name):
self.name = name
def speak(self):
return f"{self.name} makes a sound."
class Dog(Animal):
pass # inherits everything from Animal as-is
d = Dog("Rex")
print(d.speak())
Output:
Rex makes a sound.
Overriding Methods and Using super()
A child class can redefine (override) a method it inherited to give it more specific behavior. Inside the override, super() lets you still call the parent's original version instead of duplicating its logic — most commonly used inside __init__ to let the parent set up its own attributes first.
class Animal:
def __init__(self, name):
self.name = name
def speak(self):
return f"{self.name} makes a sound."
class Dog(Animal):
def __init__(self, name, breed):
super().__init__(name) # let Animal set self.name
self.breed = breed
def speak(self): # override
return f"{self.name} barks!"
d = Dog("Rex", "Labrador")
print(d.speak())
print(d.breed)
Output:
Rex barks!
Labrador
Why Use Inheritance?
Inheritance avoids duplicating code across related classes — common fields and behavior live once in the parent, and each child only adds or changes what's actually different about it. It also sets up polymorphism, where different subclasses can be used interchangeably because they all share the same parent interface (here, every Animal has a speak() method, even though each breed's version does something different).
You can see a fuller worked version of this pattern, with multiple subclasses overriding the same method, in the Python inheritance and polymorphism program example.
