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
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
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
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
1. What relationship does inheritance typically model?
2. What does super().__init__(name) do inside Dog's __init__?
3. What determines which method runs when multiple parent classes define the same method name?
4. Given class Dog(Animal), what does issubclass(Dog, Animal) return?
5. Why is Python's support for multiple inheritance considered trickier than single inheritance?
Was this page helpful?
You May Also Like
Polymorphism in Python
How different objects can respond to the same method call or operator in their own way, enabled by duck typing.
Multiple Inheritance in Python
How Python resolves method lookups across multiple parent classes using MRO (Method Resolution Order) and C3 linearization, including the diamond problem.
Abstract Classes in Python
Using the abc module to define abstract base classes and abstract methods that force subclasses to implement required behavior.
Related Reading
Related Study Notes in Programming
Browse all study notesApache Spark Study Notes
Programming · 30 topics
ProgrammingApache Flink Study Notes
Programming · 30 topics
ProgrammingHadoop Study Notes
Programming · 30 topics
ProgrammingSnowflake Study Notes
Programming · 30 topics
ProgrammingApache Airflow Study Notes
Programming · 30 topics
Programmingdbt (Data Build Tool) Study Notes
Programming · 30 topics