Python OOP Inheritance: Parent and Child Classes

Learn inheritance in Python OOP. Master subclassing, overriding methods, using super(), and multiple inheritance patterns.

Try Python OOP Inheritance Code

Overview

Inheritance is one of the foundational pillars of Object-Oriented Programming (OOP). It is a mechanism that allows a new class (known as the child or subclass) to adopt the attributes and methods of an existing class (known as the parent or superclass). This design pattern promotes code reuse, reduces redundancy, and establishes logical, hierarchical relationships between different components of your application.

To implement inheritance in Python, you pass the parent class name inside parentheses during the child class definition: `class Child(Parent):`. The child class immediately inherits all methods and variables of the parent. If the child needs custom behavior, it can override any parent method by redefining it with the same name. Inside an overridden method, you can still invoke the parent's implementation by using the built-in `super()` function, ensuring you can extend rather than completely replace existing logic.

Unlike many statically typed languages, Python natively supports Multiple Inheritance, meaning a child class can inherit from more than one parent class (e.g., `class SmartDevice(Camera, Phone):`). To resolve which parent's method executes when names conflict, Python uses a strict algorithm called the Method Resolution Order (MRO). Understanding how to structure inheritance trees and use super() is crucial for designing extensible object-oriented architectures.

Code Example

Creating a parent Vehicle class and a child Car subclass that overrides initialization using super().

inheritance.py
Try in Editor
class Vehicle:
    def __init__(self, brand, year):
        self.brand = brand
        self.year = year

    def display_info(self):
        return f"{self.year} {self.brand}"

class Car(Vehicle):
    def __init__(self, brand, year, model):
        # Call parent constructor
        super().__init__(brand, year)
        self.model = model

    def display_info(self):
        # Override parent method and extend it
        parent_info = super().display_info()
        return f"{parent_info} {self.model}"

my_car = Car("Ford", 2026, "Mustang")
print(my_car.display_info())
Terminal Output
2026 Ford Mustang

Real-world Use Cases

  • Creating custom exception classes that inherit from Exception
  • Building UI components (like Buttons) inheriting from a generic View
  • Defining hierarchical database models (e.g., User, Employee, Manager)

Frequently Asked Questions

What does super() do in subclassing?

super() returns a temporary object of the parent class, allowing you to call its methods, which is particularly useful for initializing parent fields in __init__.

Can you prevent a class from being inherited?

Python does not support a 'final' keyword like Java to restrict inheritance. Conventionally, we document class intent, or use type checkers with @final annotations from the typing module.

Keep Learning

Recommended Python Resources

Expand your knowledge with related interactive tutorials, cheat sheets, and code comparisons.