codingstuff.io
ExploreTutorialsProblemsCS Subjects
Get Started
ExploreTutorialsProblemsCS Subjects
Get Started
codingstuff.io

Master the art of building software through interactive tutorials, real-world problems, and guided projects.

Pune, Maharashtra, India

codingstuffmail@gmail.com

Product

  • Explore
  • Tutorials
  • Problems
  • CS Subjects

Company

  • About
  • Contact
  • Privacy Policy
  • Terms & Conditions
  • Sitemap

© 2026 codingstuff.io. All rights reserved.

Built with ❤️ for developers everywhere

/
/
All Tutorials
🐍

Python Programming

33 / 68 topics
30Python OOP Concepts31Python Classes & Objects32Python Class Methods & Properties33Python Inheritance & Multiple Inheritance34Python Polymorphism & Operator Overloading35Python Encapsulation
Tutorials/Python Programming/Python Inheritance & Multiple Inheritance
🐍Python Programming

Python Inheritance & Multiple Inheritance

Updated 2026-05-15
30 min read

Python Inheritance & Multiple Inheritance

Inheritance is a fundamental concept in object-oriented programming (OOP) that allows you to create new classes based on existing ones. This tutorial will cover single inheritance, multiple inheritance, the super() function, Method Resolution Order (MRO), and the diamond problem. Understanding these concepts will help you build more organized and reusable code.

Introduction

Inheritance is a mechanism where a class inherits attributes and methods from another class. This promotes code reusability and establishes an "is-a" relationship between classes. For example, a Dog class can inherit from an Animal class because a dog is an animal.

Multiple inheritance allows a class to inherit from more than one base class. While powerful, it can also introduce complexity, especially with the diamond problem where two or more base classes share a common ancestor.

Single Inheritance

Single inheritance is the simplest form of inheritance where a derived class inherits from only one base class. This is the most straightforward way to establish an "is-a" relationship.

Example: Single Inheritance

Python
1# animal.py
2class Animal:
3 def __init__(self, name):
4 self.name = name
5
6 def speak(self):
7 return "Some sound"
Python
1# dog.py
2from animal import Animal
3
4class Dog(Animal):
5 def speak(self):
6 return "Woof!"
Python
1# main.py
2from dog import Dog
3
4dog = Dog("Buddy")
5print(dog.name) # Output: Buddy
6print(dog.speak()) # Output: Woof!
Output
Buddy
Woof!

In this example, the Dog class inherits from the Animal class. The Dog class overrides the speak() method to provide its own implementation.

Multiple Inheritance

Multiple inheritance allows a class to inherit from more than one base class. This can be useful when you want to combine behaviors from different classes into a single class.

Example: Multiple Inheritance

Python
1# swimmer.py
2class Swimmer:
3 def swim(self):
4 return "Swimming fast"
Python
1# flyer.py
2class Flyer:
3 def fly(self):
4 return "Flying high"
Python
1# duck.py
2from swimmer import Swimmer
3from flyer import Flyer
4
5class Duck(Swimmer, Flyer):
6 pass
Python
1# main.py
2from duck import Duck
3
4duck = Duck()
5print(duck.swim()) # Output: Swimming fast
6print(duck.fly()) # Output: Flying high
Output
Swimming fast
Flying high

In this example, the Duck class inherits from both Swimmer and Flyer classes. It can use methods from both base classes.

The super() Function

The super() function is used to call a method from a parent class in the inheritance hierarchy. This is particularly useful when you want to extend or modify the behavior of a base class method without completely overriding it.

Example: Using super()

Python
1# animal.py
2class Animal:
3 def __init__(self, name):
4 self.name = name
5
6 def speak(self):
7 return "Some sound"
Python
1# dog.py
2from animal import Animal
3
4class Dog(Animal):
5 def __init__(self, name, breed):
6 super().__init__(name)
7 self.breed = breed
8
9 def speak(self):
10 return f"{super().speak()} and barks loudly"
Python
1# main.py
2from dog import Dog
3
4dog = Dog("Buddy", "Golden Retriever")
5print(dog.name) # Output: Buddy
6print(dog.breed) # Output: Golden Retriever
7print(dog.speak()) # Output: Some sound and barks loudly
Output
Buddy
Golden Retriever
Some sound and barks loudly

In this example, the Dog class uses super() to call the __init__() method of the Animal class. It also extends the speak() method by adding additional behavior.

Method Resolution Order (MRO)

Method Resolution Order is the order in which Python looks for a method or attribute in an inheritance hierarchy. This is important in multiple inheritance scenarios to avoid ambiguity and ensure that methods are called correctly.

Example: MRO

Python
1# animal.py
2class Animal:
3 def speak(self):
4 return "Animal sound"
Python
1# dog.py
2from animal import Animal
3
4class Dog(Animal):
5 pass
Python
1# cat.py
2from animal import Animal
3
4class Cat(Animal):
5 pass
Python
1# pet.py
2from dog import Dog
3from cat import Cat
4
5class Pet(Dog, Cat):
6 pass
Python
1# main.py
2from pet import Pet
3
4pet = Pet()
5print(pet.speak()) # Output: Animal sound
6print(Pet.mro()) # Output: [<class '__main__.Pet'>, <class 'dog.Dog'>, <class 'cat.Cat'>, <class 'animal.Animal'>, <class 'object'>]
Output
Animal sound
[<class '__main__.Pet'>, <class 'dog.Dog'>, <class 'cat.Cat'>, <class 'animal.Animal'>, <class 'object'>]

In this example, the Pet class inherits from both Dog and Cat. The MRO shows the order in which Python will look for methods. Since Dog is listed before Cat, the speak() method of Animal is called.

Diamond Problem

The diamond problem occurs when two base classes inherit from a common ancestor, and a derived class inherits from both base classes. This can lead to ambiguity about which method should be called.

Example: Diamond Problem

Python
1# animal.py
2class Animal:
3 def speak(self):
4 return "Animal sound"
Python
1# bird.py
2from animal import Animal
3
4class Bird(Animal):
5 pass
Python
1# mammal.py
2from animal import Animal
3
4class Mammal(Animal):
5 pass
Python
1# dog_bird.py
2from bird import Bird
3from mammal import Mammal
4
5class DogBird(Bird, Mammal):
6 pass
Python
1# main.py
2from dog_bird import DogBird
3
4dog_bird = DogBird()
5print(dog_bird.speak()) # Output: Animal sound
6print(DogBird.mro()) # Output: [<class '__main__.DogBird'>, <class 'bird.Bird'>, <class 'mammal.Mammal'>, <class 'animal.Animal'>, <class 'object'>]
Output
Animal sound
[<class '__main__.DogBird'>, <class 'bird.Bird'>, <class 'mammal.Mammal'>, <class 'animal.Animal'>, <class 'object'>]

In this example, the DogBird class inherits from both Bird and Mammal, which both inherit from Animal. The MRO ensures that the speak() method of Animal is called.

Practical Example

Let's create a practical example that combines multiple inheritance, the super() function, and understanding the MRO.

Example: Library System with Inheritance

Python
1# book.py
2class Book:
3 def __init__(self, title):
4 self.title = title
5
6 def display(self):
7 return f"Book: {self.title}"
Python
1# electronic_book.py
2from book import Book
3
4class ElectronicBook(Book):
5 def __init__(self, title, format):
6 super().__init__(title)
7 self.format = format
8
9 def display(self):
10 return f"{super().display()} (Format: {self.format})"
Python
1# audiobook.py
2from book import Book
3
4class Audiobook(Book):
5 def __init__(self, title, duration):
6 super().__init__(title)
7 self.duration = duration
8
9 def display(self):
10 return f"{super().display()} (Duration: {self.duration} minutes)"
Python
1# multimedia_book.py
2from electronic_book import ElectronicBook
3from audiobook import Audiobook
4
5class MultimediaBook(ElectronicBook, Audiobook):
6 def __init__(self, title, format, duration):
7 super().__init__(title, format)
8 self.duration = duration
9
10 def display(self):
11 return f"{super().display()} (Duration: {self.duration} minutes)"
Python
1# main.py
2from multimedia_book import MultimediaBook
3
4multimedia_book = MultimediaBook("Python Programming", "PDF", 120)
5print(multimedia_book.display()) # Output: Book: Python Programming (Format: PDF) (Duration: 120 minutes)
6print(MultimediaBook.mro()) # Output: [<class '__main__.MultimediaBook'>, <class 'electronic_book.ElectronicBook'>, <class 'audiobook.Audiobook'>, <class 'book.Book'>, <class 'object'>]
Output
Book: Python Programming (Format: PDF) (Duration: 120 minutes)
[<class '__main__.MultimediaBook'>, <class 'electronic_book.ElectronicBook'>, <class 'audiobook.Audiobook'>, <class 'book.Book'>, <class 'object'>]

In this example, the MultimediaBook class inherits from both ElectronicBook and Audiobook. The MRO ensures that methods are called in the correct order, and the super() function is used to initialize attributes from base classes.

Summary

ConceptDescription
Single InheritanceA class inherits from a single base class.
Multiple InheritanceA class inherits from more than one base class.
super() FunctionUsed to call methods from a parent class in the inheritance hierarchy.
Method Resolution Order (MRO)The order in which Python looks for a method or attribute.
Diamond ProblemAmbiguity that occurs when two base classes inherit from a common ancestor.

What's Next?

In the next tutorial, we will explore polymorphism and operator overloading, which allow objects of different classes to be treated as objects of a common superclass. This will further enhance your understanding of how Python handles object-oriented programming.

Stay tuned for more advanced topics in OOP with Python!


PreviousPython Class Methods & PropertiesNext Python Polymorphism & Operator Overloading

Recommended Gear

Python Class Methods & PropertiesPython Polymorphism & Operator Overloading