Python Inheritance

In this tutorial, you will learn about python inheritance

Introduction to Inheritance


Inheritance is a fundamental concept in object-oriented programming (OOP) that allows a class to inherit properties and methods from another class.

This promotes code reuse, modularity, and the creation of a hierarchical structure in your code.

In Python, inheritance is implemented using the class keyword.

class ParentClass:
    # Attributes and methods of the parent class
    pass

class ChildClass(ParentClass):
    # Additional attributes and methods of the child class
    pass

In this tutorial, we will explore the various aspects of Python inheritance, including its syntax, types of inheritance, method overriding, and the use of super().

 

10.2 Types of Inheritance


Python supports three types of inheritance:

 

10.2.1 Single Inheritance

Single inheritance involves a child class inheriting from only one parent class.

class Parent:
    # Parent class code

class Child(Parent):
    # Child class inherits from Parent

 

10.2.2 Multiple Inheritance

Multiple inheritance allows a child class to inherit from more than one parent class.

class Parent1:
    # Code of the first parent class

class Parent2:
    # Code of the second parent class

class Child(Parent1, Parent2):
    # Child class inherits from both Parent1 and Parent2

 

10.2.3 Multilevel Inheritance

In multilevel inheritance, a class inherits from another class, and then another class inherits from the derived class.

class Grandparent:
    # Code of the grandparent class

class Parent(Grandparent):
    # Code of the parent class, inherits from Grandparent

class Child(Parent):
    # Code of the child class, inherits from Parent

 

10.3 Method Overriding


Method overriding occurs when a child class provides a specific implementation for a method that is already defined in its parent class. This allows customization of behavior in the child class.

class Parent:
    def show_message(self):
        print("Message from the parent class")

class Child(Parent):
    def show_message(self):
        print("Message from the child class")

# Example usage
child_obj = Child()
child_obj.show_message()  # Output: Message from the child class

 

10.4 Using super()


The super() function is used to call a method from the parent class within the child class. It is often used in conjunction with method overriding.

class Parent:
    def show_message(self):
        print("Message from the parent class")

class Child(Parent):
    def show_message(self):
        super().show_message()
        print("Additional message from the child class")

# Example usage
child_obj = Child()
child_obj.show_message()
# Output:
# Message from the parent class
# Additional message from the child class

 

10.5 Conclusion


Inheritance is a powerful feature in Python that facilitates code reuse and the creation of well-organized and modular code. Understanding the types of inheritance, method overriding, and the use of super() can greatly enhance your ability to design and implement effective object-oriented solutions in Python.
 

© 2022-2023 All rights reserved.