Python tutorials > Object-Oriented Programming (OOP) > Inheritance > What are single/multiple inheritance?
What are single/multiple inheritance?
Single Inheritance: Introduction
Single Inheritance: Code Example
Dog
class inherits from the Animal
class. The Dog
class inherits the __init__
method (which initializes the name
attribute) and the speak
method from the Animal
class. The Dog
class also overrides the speak
method to provide a specific sound for dogs. The super().__init__(name)
call in the Dog
's __init__
ensures the parent class's initialization is performed.
class Animal:
def __init__(self, name):
self.name = name
def speak(self):
print("Generic animal sound")
class Dog(Animal):
def __init__(self, name, breed):
super().__init__(name)
self.breed = breed
def speak(self):
print("Woof!")
my_dog = Dog("Buddy", "Golden Retriever")
print(my_dog.name)
print(my_dog.breed)
my_dog.speak()
Multiple Inheritance: Introduction
Multiple Inheritance: Code Example
Amphibian
class inherits from both the Swimmer
and Walker
classes. As a result, an instance of the Amphibian
class can call both the swim()
and walk()
methods. Python uses the Method Resolution Order (MRO) to determine the order in which methods are inherited when there are conflicts (e.g., if both parent classes have a method with the same name). The MRO can be checked using the __mro__
attribute of the class.
class Swimmer:
def swim(self):
print("Swimming...")
class Walker:
def walk(self):
print("Walking...")
class Amphibian(Swimmer, Walker):
pass
frog = Amphibian()
frog.swim()
frog.walk()
Method Resolution Order (MRO)
C
inherits from both A
and B
, both of which define a method called method
. When we call c.method()
, Python uses the MRO to determine which method to call. In this case, it calls A
's method first because A
appears before B
in the class definition of C
. The output of C.__mro__
shows the resolution order: (
.
class A:
def method(self):
print("A method")
class B:
def method(self):
print("B method")
class C(A, B):
pass
c = C()
c.method()
print(C.__mro__)
Concepts behind the snippet
Real-Life Use Case Section
Best Practices
Interview Tip
When to use them
Memory footprint
Alternatives
Pros of Single Inheritance
Cons of Single Inheritance
Pros of Multiple Inheritance
Cons of Multiple Inheritance
FAQ
-
What is the 'diamond problem' in multiple inheritance?
The diamond problem occurs when a class inherits from two classes that both inherit from a common base class. This creates a diamond-shaped inheritance hierarchy, and the derived class can inherit multiple copies of the base class's attributes and methods, leading to ambiguity about which version to use. Python resolves this using the Method Resolution Order (MRO), but understanding the MRO is crucial to avoid unexpected behavior. -
When should I prefer composition over inheritance?
You should prefer composition over inheritance when you want to achieve code reuse without creating a tight coupling between classes. Composition is more flexible and avoids the complexities and potential issues associated with inheritance, such as the diamond problem. In composition, a class contains instances of other classes as attributes, allowing you to combine functionalities without inheriting their implementations. -
How does Python resolve method name conflicts in multiple inheritance?
Python uses the Method Resolution Order (MRO) to determine the order in which methods are inherited in multiple inheritance. The MRO is a deterministic order that ensures that methods are inherited in a predictable and consistent manner. You can view the MRO of a class using the__mro__
attribute.