In Python, inheritance types include single, multiple, multilevel, hierarchical, and hybrid. Single inheritance allows a class to inherit from one parent, while multiple inheritance derives from more than one base class. Multilevel involves a chain of inheritance, hierarchical stems from one parent to multiple children, and hybrid combines two or more types.
Single Inheritance
Single inheritance in Python allows a derived class to inherit properties and behavior from a single parent class. This type of inheritance facilitates code reuse and the extension of class functionalities. In single inheritance, the child class inherits attributes and methods from one parent class, enabling it to customize or enhance the inherited features.
Example:
class Parent:
def __init__(self):
self.value = "Inside Parent"
def show(self):
print(self.value)
class Child(Parent):
def __init__(self):
super().__init__()
self.value = "Inside Child"
def display(self):
print(self.value)
# Creating an object of Child
child = Child()
child.show() # Accesses method from Parent class
child.display() # Accesses method from Child class
In this code, Child inherits from Parent, demonstrating single inheritance. The Child class extends the Parent class by overriding the value attribute and adding a new method display()
.
Multiple Inheritance
Multiple inheritance in Python is a feature that allows a class to inherit attributes and methods from more than one parent class. This type of inheritance enables the creation of a more flexible and modular code structure. Python handles multiple inheritance by using a method resolution order (MRO) algorithm, which defines the order in which base classes are searched when executing a method.
To implement multiple inheritance, simply separate the parent classes with commas in the class definition. For instance.
class ParentClass1:
def method1(self):
print("Method in ParentClass1")
class ParentClass2:
def method2(self):
print("Method in ParentClass2")
class ChildClass(ParentClass1, ParentClass2):
def child_method(self):
print("Method in ChildClass")
# Creating an instance of ChildClass
child = ChildClass()
child.method1() # Accessing method from ParentClass1
child.method2() # Accessing method from ParentClass2
This approach allows ChildClass
to utilize functionalities from both ParentClass1
and ParentClass2
, demonstrating the power and flexibility of multiple inheritance in Python.
Multilevel Inheritance
Multilevel inheritance in Python occurs when a class is derived from a child class, making it a grandchild of the original parent class. This structure enables a hierarchy of classes that inherit properties and methods through multiple levels. It facilitates extended functionality and attribute sharing across generations of classes.
Example:
class Parent:
def show(self):
print("Parent method")
class Child(Parent):
def display(self):
print("Child method")
class GrandChild(Child):
def reveal(self):
print("GrandChild method")
gc = GrandChild()
gc.show() # Accesses Parent method
gc.display() # Accesses Child method
gc.reveal() # Accesses its own method
In this example, GrandChild
inherits from Child, which in turn inherits from Parent, demonstrating multilevel inheritance. This allows GrandChild
to use methods from both its parent (Child) and grandparent (Parent).
Hierarchical Inheritance
Hierarchical inheritance in Python occurs when multiple child classes inherit from a single parent class. This type of inheritance allows for the distribution of common attributes and methods among various subclasses, enhancing code reuse and organization. Each subclass can further extend or override the inherited features, tailoring them to specific needs.
For example.
class Parent:
def common_method(self):
print("This is a parent method.")
class Child1(Parent):
def child1_method(self):
print("This is Child1 specific method.")
class Child2(Parent):
def child2_method(self):
print("This is Child2 specific method.")
In this scenario, both Child1
and Child2
inherit the common_method
from Parent, demonstrating hierarchical inheritance. Each child class also introduces a unique method, showcasing their ability to expand upon the base functionality.
Hybrid Inheritance
Hybrid inheritance in Python combines two or more types of inheritance, such as single, multiple, multilevel, or hierarchical inheritance, within a single program. This approach enables more complex relationships between classes and enhances the flexibility of the code structure. For instance, a class can inherit from multiple classes (multiple inheritance) and those classes can have their own parent classes (multilevel inheritance).
Consider the following coding example to illustrate hybrid inheritance.
class Parent:
pass
class Child1(Parent):
pass
class Child2(Parent):
pass
class Grandchild(Child1, Child2):
pass
In this example, Grandchild
inherits from both Child1
and Child2
, showcasing multiple inheritance, while Child1
and Child2
inherit from Parent, demonstrating a simple form of hierarchical inheritance. This hybrid model allows Grandchild to utilize attributes and methods from all parent and grandparent classes, providing a versatile way to structure complex relationships in Python programs.