Inheritance is one of the coolest OOP features in Python. It allows you to create new classes based on existing ones to reuse to modify them. Today, we will learn about various types of inheritance allowed in Python.
Types of Inheritance in Python
There are various types of inheritance available in Python:
1) Single Inheritance
One base class and one derived class are involved in single inheritance. The characteristics and methods of the base class are passed down to the derived class. It enables the derived class to extend or modify the underlying classes behaviour.
An example code for single-level inheritance in Python:
class Animal: def __init__(self, name): self.name = name def eat(self): print(f"{self.name} is eating.") class Dog(Animal): def bark(self): print("Woof!") my_dog = Dog("Buddy") my_dog.eat() # Output: Buddy is eating. my_dog.bark() # Output: Woof!
2) Multiple Inheritance
Multiple inheritance entails more than one base class and one derived class. All of the base classes characteristics and methods are passed down to the derived class. It allows the derived class to aggregate and use functionality from several sources.
An example code for multiple inheritance in Python:
class Animal: def __init__(self, name): self.name = name def eat(self): print(f"{self.name} is eating.") class Mammal: def __init__(self, species): self.species = species def give_birth(self): print(f"{self.species} is giving birth.") class Dolphin(Animal, Mammal): def swim(self): print("Dolphin is swimming.") my_dolphin = Dolphin("Flipper") my_dolphin.eat() # Output: Flipper is eating. my_dolphin.give_birth() # Output: Flipper is giving birth. my_dolphin.swim() # Output: Dolphin is swimming.
3) Multilevel Inheritance
A chain of inheritance with numerous levels is referred to as multilevel inheritance. A derived class inherits from a base class, which in turn inherits from another derived class. This results in a hierarchical structure in which each class inherits its parent classes characteristics and methods.
An example code for multilevel inheritance in Python:
class Animal: def __init__(self, name): self.name = name def eat(self): print(f"{self.name} is eating.") class Mammal(Animal): def __init__(self, name, species): super().__init__(name) self.species = species def give_birth(self): print(f"{self.species} is giving birth.") class Dolphin(Mammal): def swim(self): print("Dolphin is swimming.") my_dolphin = Dolphin("Flipper", "Bottlenose Dolphin") my_dolphin.eat() # Output: Flipper is eating. my_dolphin.give_birth() # Output: Bottlenose Dolphin is giving birth. my_dolphin.swim() # Output: Dolphin is swimming.
4) Hierarchical inheritance
Multiple derived classes inherit from a single base class in a hierarchical inheritance. It generates a tree-like structure in which several classes inherit from a single base class. Each derived class inherits the base class attributes and methods while also having its own unique attributes and methods.
An example code for hierarchical inheritance in Python:
class Animal: def __init__(self, name): self.name = name def eat(self): print(f"{self.name} is eating.") class Dog(Animal): def bark(self): print("Woof!") class Cat(Animal): def meow(self): print("Meow!") my_dog = Dog("Buddy") my_dog.eat() # Output: Buddy is eating. my_dog.bark() # Output: Woof! my_cat = Cat("Whiskers") my_cat.eat() # Output: Whiskers is eating. my_cat.meow() # Output: Meow!
5) Hybrid inheritance
The term "hybrid inheritance" refers to the combination of multiple inheritance and multilevel inheritance. It enables a customizable class hierarchy with a mix of features from several inheritance types. When a class needs to inherit from numerous base classes and participate in a multilayer inheritance chain, this type of inheritance is beneficial.
An example code for hybrid inheritance in Python:
class A: def method_a(self): print("Method A") class B(A): def method_b(self): print("Method B") class C(A): def method_c(self): print("Method C") class D(B, C): def method_d(self): print("Method D") my_object = D() my_object.method_a() # Output: Method A my_object.method_b() # Output: Method B my_object.method_c() # Output: Method C my_object.method_d() # Output: Method D
How many levels of Inheritance are allowed in Python?
There is no hard limit to the number of levels of inheritance that can exist in Python. You can have as many degrees of inheritance as you need to meet the requirements of your programme. However, in order to preserve clarity and prevent unnecessary complexity, your class hierarchy must be properly designed.
The number of layers of inheritance relates to the depth of the class hierarchy, from which each derived class inherits. In a multilayer inheritance scenario, for example, there could be a base class, a derived class that inherits from the base class, and another derived class that inherits from the intermediate derived class. This results in a hierarchical structure with two levels of inheritance.
An example code to illustrate different levels of inheritance:
class Animal: def eat(self): print("Eating...") class Mammal(Animal): def sleep(self): print("Sleeping...") class Dog(Mammal): def bark(self): print("Woof!") class Bulldog(Dog): def guard(self): print("Guarding...") my_dog = Bulldog() my_dog.eat() # Output: Eating... my_dog.sleep() # Output: Sleeping... my_dog.bark() # Output: Woof! my_dog.guard() # Output: Guarding…
Conclusion
So, whether you're using single inheritance to build straightforward class hierarchies, multiple inheritance to combine functionalities, or diving into more advanced forms like multilevel and hierarchical inheritance, each of them brings its advantages depending on the scenario.