9 4 3 Multilevel Inheritance Explained
Key Concepts
Multilevel inheritance in Python involves several key concepts:
- Base Class
- Intermediate Class
- Derived Class
- Method Resolution Order (MRO)
- Overriding Methods
1. Base Class
The base class is the initial class from which other classes inherit. It serves as the foundation for the inheritance hierarchy.
Example:
class Animal: def __init__(self, name): self.name = name def speak(self): return "Some sound"
Analogy: Think of the base class as the original blueprint for a house, which defines the basic structure and features.
2. Intermediate Class
The intermediate class inherits from the base class and can also be inherited by other classes. It adds additional features or modifies existing ones.
Example:
class Mammal(Animal): def __init__(self, name, type): super().__init__(name) self.type = type def speak(self): return "Mammal sound"
Analogy: Think of the intermediate class as a modified blueprint for a house, which adds extra rooms or features while still retaining the basic structure.
3. Derived Class
The derived class inherits from the intermediate class, forming a multilevel inheritance hierarchy. It can access attributes and methods from both the intermediate and base classes.
Example:
class Dog(Mammal): def __init__(self, name, type, breed): super().__init__(name, type) self.breed = breed def speak(self): return "Woof!"
Analogy: Think of the derived class as a specific type of house built from the modified blueprint, which adds even more unique features.
4. Method Resolution Order (MRO)
MRO determines the order in which methods are inherited in a multilevel inheritance hierarchy. It ensures that methods are called from the most specific class first.
Example:
print(Dog.mro()) # Output: [Dog, Mammal, Animal, object]
Analogy: Think of MRO as the order in which different versions of a recipe are followed, starting with the most specific version and moving to more general ones if needed.
5. Overriding Methods
Overriding methods allows a derived class to provide a specific implementation of a method that is already defined in its parent classes.
Example:
class Cat(Mammal): def __init__(self, name, type, color): super().__init__(name, type) self.color = color def speak(self): return "Meow!"
Analogy: Think of overriding methods as customizing a standard feature in a house, such as changing the color of the walls in a room.
Putting It All Together
By understanding and using multilevel inheritance effectively, you can create complex and hierarchical class structures in Python.
Example:
class Animal: def __init__(self, name): self.name = name def speak(self): return "Some sound" class Mammal(Animal): def __init__(self, name, type): super().__init__(name) self.type = type def speak(self): return "Mammal sound" class Dog(Mammal): def __init__(self, name, type, breed): super().__init__(name, type) self.breed = breed def speak(self): return "Woof!" class Cat(Mammal): def __init__(self, name, type, color): super().__init__(name, type) self.color = color def speak(self): return "Meow!" dog = Dog("Buddy", "Dog", "Golden Retriever") cat = Cat("Whiskers", "Cat", "Calico") print(dog.name, dog.speak()) # Output: Buddy Woof! print(cat.name, cat.speak()) # Output: Whiskers Meow!