Python's Inheritance Types
Python supports various types of inheritance that provide flexibility in creating class hierarchies and promoting code reuse. In this article, we will delve into the different types of inheritance in Python, understand their syntax and characteristics, and explore how they can be used to design flexible and modular code structures.
Single Inheritance :
Single inheritance is the simplest form of inheritance, where a class is inherited from a single base class. The derived class inherits all the attributes and methods of the base class and can extend or override them as needed.
Single inheritance is commonly used when we want to create a specialized version of an existing class or when we want to add additional functionality to an existing class.
Syntax:
class BaseClass:
# Base class definition
class DerivedClass(BaseClass):
# Derived class definition
In this syntax, 'BaseClass' is the name of the base class, and 'DerivedClass' is the name of the derived class.
Example:
class Vehicle:
def move(self):
print("Moving on land")
class Car(Vehicle):
def honk(self):
print("Honking the horn")
# Creating an instance of the Car class
car = Car()
car.move() # Output: Moving on land
car.honk() # Output: Honking the horn
In this example, the 'Car' class is inherited from the 'Vehicle' class, allowing it to access and extend the 'move()'
method while adding its own 'honk()' method.
Below is the Flow Diagram for better clarification:
Multiple Inheritance :
Multiple inheritance allows a class to inherit from multiple base classes. This type of inheritance enables the derived class to inherit
attributes and methods from multiple sources, promoting code reuse and flexibility. However, careful consideration is required to handle
potential naming conflicts and maintain code clarity.
Multiple inheritance can be useful when we want to combine functionality from multiple classes or when we want to create a class that exhibits
characteristics of different categories.
Syntax:
class BaseClass1:
# Base class 1 definition
class BaseClass2:
# Base class 2 definition
class DerivedClass(BaseClass1, BaseClass2):
# Derived class definition
In this syntax, 'BaseClass1' and 'BaseClass2' are the base classes, and 'DerivedClass' is the derived
class inheriting from both base classes.
Example:
class Animal:
def eat(self):
print("Eating")
class Flyable:
def fly(self):
print("Flying")
class Bird(Animal, Flyable):
def chirp(self):
print("Chirping")
# Creating an instance of the Bird class
bird = Bird()
bird.eat() # Output: Eating
bird.fly() # Output: Flying
bird.chirp() # Output: Chirping
In this example, the 'Bird' class inherits from both the 'Animal' and 'Flyable' classes, allowing
it to access methods from both classes.
Below is the Flow Diagram for better clarification:
Multilevel Inheritance :
Multilevel inheritance involves a chain of inheritance, where a derived class becomes the base class for another class. This type of inheritance
allows for the creation of a hierarchy of classes, where each derived class inherits attributes and methods from its immediate parent class.
In simple words Multilevel inheritance allows us to create specialized classes that inherit and build upon the functionality of their parent classes.
Example:
class Vehicle:
def move(self):
print("Moving on land")
class Car(Vehicle):
def honk(self):
print("Honking the horn")
class SportsCar(Car):
def accelerate(self):
print("Accelerating")
# Creating an instance of the SportsCar class
sports_car = SportsCar()
sports_car.move() # Output: Moving on land
sports_car.honk() # Output: Honking the horn
sports_car.accelerate()# Output: Accelerating
In this example, the 'SportsCar' class inherits from the 'Car' class, which in turn inherits from the 'Vehicle' class. The 'SportsCar' class
has access to methods from both its parent classes.
Below is the Flow Diagram for better clarification:
Hierarchical Inheritance :
Hierarchical inheritance involves multiple classes inheriting from a single base class. In this type of inheritance, multiple derived
classes inherit attributes and methods from a common base class.
Hierarchical inheritance can be useful in scenarios where we need to create different specialized classes that share common functionality
and attributes..
Example:
class Animal:
def eat(self):
print("Eating")
class Dog(Animal):
def bark(self):
print("Barking")
class Cat(Animal):
def meow(self):
print("Meowing")
# Creating instances of the Dog and Cat classes
dog = Dog()
cat = Cat()
dog.eat() # Output: Eating
cat.eat() # Output: Eating
dog.bark() # Output: Barking
cat.meow() # Output: Meowing
In this example, both the 'Dog' and 'Cat' classes inherit the 'eat()' method from the 'Animal' base class. Each derived class also has its unique methods.
Below is the Flow Diagram for better clarification:
Hybrid Inheritance :
Hybrid inheritance is a combination of multiple types of inheritance, such as single inheritance, multiple inheritance, or multilevel inheritance.
It allows for the creation of complex class hierarchies that incorporate the benefits of different types of inheritance.
Hybrid inheritance generally allows us to create complex class hierarchies that can exhibit multiple types of inheritance relationships.
Here's an example for better understanding:
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")
# Creating an instance of the D class
d = D()
d.method_a() # Output: Method A
d.method_b() # Output: Method B
d.method_c() # Output: Method C
d.method_d() # Output: Method D
In this example, the 'D' class inherits from both the 'B' and 'C' classes, which, in
turn, inherit from the 'A' class. The 'D' class can access methods from all three classes.
Below is the Flow Diagram for better clarification: