Inheritance in Python allows a class (called a child class or subclass) to derive or inherit the attributes and methods from another class (called a parent class or base class). This concept promotes code reuse and makes it easier to create and maintain applications by enabling hierarchical class relationships.

Key Features of Inheritance

  1. Child Class Can Access Parent Class Members:

    Attributes and methods defined in the parent class are accessible in the child class.

  2. Child Class Can Override Parent Class Members:

    A child class can redefine methods or attributes of the parent class.

  3. Support for Multiple Inheritance:

    Python allows a child class to inherit from multiple parent classes.

class ParentClass:
    def __init__(self, value):
        self.value = value
    
    def parent_method(self):
        print("This is a method in the ParentClass")

# Inheriting ParentClass
class ChildClass(ParentClass):
    def child_method(self):
        print("This is a method in the ChildClass")

# Example usage
child = ChildClass(10)
child.parent_method()  # Accessing parent method
child.child_method()   # Accessing child method

Types of Inheritance in Python

  1. Single Inheritance: One child class inherits from one parent class.
class Parent:
    pass

class Child(Parent):
    pass
  1. Multiple Inheritance: A child class inherits from more than one parent class.
class Parent1:
    pass

class Parent2:
    pass

class Child(Parent1, Parent2):
    pass
  1. Multilevel Inheritance: A class inherits from a child class, creating a chain.
class GrandParent:
    pass

class Parent(GrandParent):
    pass

class Child(Parent):
    pass
  1. Hierarchical Inheritance: Multiple child classes inherit from a single parent class.
class Parent:
    pass

class Child1(Parent):
    pass

class Child2(Parent):
    pass
  1. Hybrid Inheritance: A combination of multiple and hierarchical inheritance.