Python Guide Sidebar

Python – Inheritance

Inheritance is one of the fundamental concepts of Object-Oriented Programming (OOP) in Python. It allows a class (child class) to inherit attributes and methods from another class (parent class). This promotes code reuse, modularity, and a hierarchical structure in programming.

What is Inheritance?

Inheritance enables a new class to derive properties and behaviors from an existing class. This means that the child class gets access to the methods and attributes of the parent class, reducing redundancy and improving maintainability.

Key Benefits of Inheritance:

Code Reusability – Avoid rewriting code by using properties of an existing class.
Scalability – Easily extend functionalities in large applications.
Modularity – Organize code efficiently with hierarchical relationships.
Improved Maintainability – Centralize modifications in a base class instead of changing multiple classes.

Types of Inheritance in Python

1. Single Inheritance

A child class inherits from a single parent class.

Example:

class Animal:
    def speak(self):
        return "Some sound"

class Dog(Animal):
    def speak(self):
        return "Bark"

d = Dog()
print(d.speak())  # Output: Bark

The Dog class inherits from Animal and overrides the speak() method.

2. Multiple Inheritance

A child class inherits from more than one parent class.

Example:

class Parent1:
    def method1(self):
        return "Method from Parent1"

class Parent2:
    def method2(self):
        return "Method from Parent2"

class Child(Parent1, Parent2):
    pass

c = Child()
print(c.method1())  # Output: Method from Parent1
print(c.method2())  # Output: Method from Parent2

The Child class inherits methods from both Parent1 and Parent2.

3. Multilevel Inheritance

A child class inherits from another child class, forming a chain.

Example:

class Grandparent:
    def grand_method(self):
        return "Grandparent method"

class Parent(Grandparent):
    def parent_method(self):
        return "Parent method"

class Child(Parent):
    def child_method(self):
        return "Child method"

c = Child()
print(c.grand_method())  # Output: Grandparent method
print(c.parent_method())  # Output: Parent method
print(c.child_method())   # Output: Child method

The Child class inherits from Parent, which in turn inherits from Grandparent.

4. Hierarchical Inheritance

Multiple child classes inherit from the same parent class.

Example:

class Parent:
    def parent_method(self):
        return "Parent method"

class Child1(Parent):
    pass

class Child2(Parent):
    pass

c1 = Child1()
c2 = Child2()

print(c1.parent_method())  # Output: Parent method
print(c2.parent_method())  # Output: Parent method

5. Hybrid Inheritance

A combination of different types of inheritance.

Example:

class Base:
    def base_method(self):
        return "Base method"

class A(Base):
    def a_method(self):
        return "A method"

class B(Base):
    def b_method(self):
        return "B method"

class C(A, B):
    def c_method(self):
        return "C method"

c = C()
print(c.base_method())  # Output: Base method
print(c.a_method())     # Output: A method
print(c.b_method())     # Output: B method
print(c.c_method())     # Output: C method

Method Resolution Order (MRO)

In multiple inheritance, Python follows the C3 Linearization Algorithm to determine the order in which methods are inherited. The mro() method helps check this order.

Example:

class A:
    pass

class B(A):
    pass

class C(A):
    pass

class D(B, C):
    pass

print(D.mro())

Overriding Methods in Inheritance

A child class can override a method from its parent class to provide a different implementation.

class Parent:
    def show(self):
        return "Parent method"

class Child(Parent):
    def show(self):
        return "Child method"

c = Child()
print(c.show())  # Output: Child method

Using super() to Access Parent Class Methods

The super() function allows you to call a method from the parent class inside the child class.

Example:

class Parent:
    def show(self):
        return "Parent method"

class Child(Parent):
    def show(self):
        return super().show() + " - Extended in Child"

c = Child()
print(c.show())  # Output: Parent method - Extended in Child

Conclusion

Inheritance is a powerful feature in Python that enables code reuse and a structured approach to object-oriented programming. By understanding different types of inheritance, method overriding, and super(), you can design more efficient and scalable programs.

Additional Resources

🔹 Python Official Documentation