100% Free Forever
AI-Powered Learning
Industry Expert Content
Certificates & Badges
Learn At Your Own Pace
Python

Inheritance in Python

How subclasses reuse and extend behavior from parent classes, and how super() and MRO resolve method lookups.

OOP BasicsIntermediate10 min readJul 7, 2026
Analogies

1. Introduction

Inheritance lets a class (the child or subclass) reuse attributes and methods defined in another class (the parent or superclass), while adding or overriding behavior of its own. It models "is-a" relationships — for example, a Dog is an Animal, so Dog can inherit shared behavior like eat() from Animal while defining its own bark().

🏏

Cricket analogy: Just as an all-rounder like Ravindra Jadeja is-a bowler who also inherits a batsman's core skills while adding his own spin-bowling specialty, a subclass inherits a parent's shared behavior while adding methods of its own.

Inheritance promotes code reuse and establishes a natural hierarchy: common functionality lives once in the base class, and specialized classes extend or customize it without duplicating code.

🏏

Cricket analogy: Just as a cricket academy teaches the base technique of grip and stance once to every player, letting specialists like fast bowlers and spinners each build their own approach on top, inheritance keeps common functionality in one base class instead of duplicating it.

2. Syntax

python
class Parent:
    def __init__(self, name):
        self.name = name

    def greet(self):
        return f"Hello, I am {self.name}"


class Child(Parent):
    def __init__(self, name, age):
        super().__init__(name)   # call the parent's __init__
        self.age = age

    def greet(self):             # override the parent's method
        base = super().greet()
        return f"{base}, age {self.age}"

3. Explanation

A subclass is declared by listing the parent class in parentheses after the subclass name: class Child(Parent):. The subclass automatically gains all of the parent's attributes and methods. super() returns a proxy object that lets you call the parent class's methods (most commonly __init__) without hardcoding the parent's name, which keeps the code robust if the hierarchy changes.

🏏

Cricket analogy: Just as a young all-rounder joining the national team automatically inherits the squad's standard training regimen, and can still call on the specialist coach's original drills via a direct request rather than reinventing them, class Child(Parent) gains the parent's methods and super() lets it call the parent's __init__ directly.

When a method is defined in both the parent and the child with the same name, the child's version overrides the parent's for instances of the child. This lets subclasses customize inherited behavior while still being able to call back to the original implementation via super().

🏏

Cricket analogy: Just as a specialist death-over bowler like Jasprit Bumrah executes the team's standard bowling plan but overrides it with his own yorker variation in the final over, while still following the base plan via a coach's original instructions, a child class overrides a parent method while calling back via super().

Python supports multiple inheritance (a class inheriting from more than one parent), unlike languages such as Java. When multiple parents define the same method, Python resolves which one wins using the Method Resolution Order (MRO), computed with the C3 linearization algorithm and inspectable via ClassName.__mro__ or ClassName.mro(). super() follows this MRO chain, not simply 'the one parent class' — which matters a lot in diamond-shaped multiple-inheritance hierarchies.

You can check an inheritance relationship with isinstance(obj, ParentClass) (True for both Parent and Child instances) and issubclass(Child, Parent) (True). This is more flexible than exact type-checking with type(obj) is ParentClass, which would be False for a Child instance.

4. Example

python
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)
        self.breed = breed

    def speak(self):
        base = super().speak()
        return f"{base}, specifically {self.name} the {self.breed} barks"


d = Dog("Rex", "Labrador")
print(d.speak())
print(isinstance(d, Animal))
print(issubclass(Dog, Animal))
print(Dog.__mro__)

5. Output

text
Rex makes a sound, specifically Rex the Labrador barks
True
True
(<class '__main__.Dog'>, <class '__main__.Animal'>, <class 'object'>)

6. Key Takeaways

  • Inheritance lets a subclass reuse and extend a parent class's attributes and methods.
  • super() calls the parent's implementation without hardcoding the parent class's name.
  • A subclass can override a parent method by redefining it with the same name.
  • Python supports multiple inheritance, resolved via the Method Resolution Order (MRO).
  • isinstance() and issubclass() are the idiomatic ways to check inheritance relationships.

Practice what you learned

Was this page helpful?

Topics covered

#Python#PythonProgrammingStudyNotes#Programming#InheritanceInPython#Inheritance#Syntax#Explanation#Example#OOP#StudyNotes#SkillVeris