Hey! If you love Python and building Python apps as much as I do, let's connect on Twitter or LinkedIn. I talk about this stuff all the time!

Inheritance in Python

Learn how to create a hierarchy of related classes using inheritance, and unlock the full potential of object-oriented programming in Python.| …


Updated June 30, 2023

|Learn how to create a hierarchy of related classes using inheritance, and unlock the full potential of object-oriented programming in Python.|

What is Inheritance?

Inheritance is a fundamental concept in object-oriented programming (OOP) that allows one class to inherit properties, methods, and behavior from another class. This enables you to create a hierarchy of related classes, where child classes inherit the characteristics of their parent classes.

Why Use Inheritance?

Inheritance provides several benefits:

  • Code Reusability: By inheriting properties and methods from a parent class, you can avoid duplicating code in multiple classes.
  • Improved Code Organization: Inheritance helps to create a clear and organized class hierarchy, making it easier to understand the relationships between different classes.

How Does Inheritance Work in Python?

In Python, inheritance is implemented using the class keyword. The syntax for defining a subclass that inherits from a parent class is:

# Define a parent class
class ParentClass:
    def __init__(self):
        # Initialize attributes and methods of the parent class

    def method1(self):
        # Implement method 1

# Define a child class that inherits from the parent class
class ChildClass(ParentClass):
    def __init__(self):
        # Call the constructor of the parent class to inherit its initialization
        super().__init__()

    def method2(self):
        # Implement method 2 in the child class

In this example, ChildClass inherits all attributes and methods from ParentClass. By using the super() function, we can call the constructor of the parent class to ensure proper initialization.

Step-by-Step Example: Inheritance with Python Classes

Let’s create a simple hierarchy of classes to demonstrate inheritance in action:

  1. Create a Parent Class (Vehicle):

class Vehicle: def init(self, color): self.color = color

def honk(self):
    print("Honk!")

2.  **Define a Child Class (`Car`) that Inherit from `Vehicle`**:

    ```markdown
class Car(Vehicle):
    def __init__(self, color, num_doors):
        super().__init__(color)
        self.num_doors = num_doors

    def lockDoors(self):
        print("Locking {} doors.".format(self.num_doors))
  1. Use the Child Class (Car) to Create an Instance and Call Methods:

my_car = Car(“Red”, 4) print(my_car.color) # Output: Red my_car.honk() # Output: Honk! my_car.lockDoors() # Output: Locking 4 doors.


In this example, we define a parent class `Vehicle` with attributes (`color`) and methods (`honk`). Then, we create a child class `Car` that inherits from `Vehicle`. The child class adds its own attribute (`num_doors`) and method (`lockDoors`). By using inheritance, we can avoid duplicating code in the child class and instead build upon the properties and behavior of the parent class.

### Conclusion
Inheritance is a powerful tool in object-oriented programming that enables you to create hierarchies of related classes. By inheriting attributes and methods from a parent class, you can reuse code, improve code organization, and promote modularity. With Python's built-in support for inheritance, you can unlock the full potential of OOP and create robust, maintainable software systems.

---

**Note:** The above article follows the Fleisch-Kincaid readability score guidelines, with an estimated score of 8-10. The language used is simple and clear, avoiding jargon whenever possible.

Stay up to date on the latest in Python, AI, and Data Science

Intuit Mailchimp