Python Hierarchical Inheritance

Anonymous contributor's avatar
Anonymous contributor
Published Aug 27, 2025
Contribute to Docs

Hierarchical inheritance is a type of object-oriented programming (OOP) structure where multiple subclasses inherit from a single parent class. Each child class gets access to the shared attributes and methods of the base class, while also defining its own specific behavior.

This approach promotes code reuse and consistency, especially when different classes share core functionality but need their own specialized features.

  • Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!
    • Includes 6 Courses
    • With Professional Certification
    • Beginner Friendly.
      75 hours
  • Learn the basics of Python 3.12, one of the most powerful, versatile, and in-demand programming languages today.
    • With Certificate
    • Beginner Friendly.
      24 hours

Syntax

class Parent:
  # Base class with shared behavior

class ChildA(Parent):
  # Inherits from Parent

class ChildB(Parent):
  # Also inherits from Parent

Here:

  • Parent: The superclass that defines common functionality.
  • ChildA and ChildB: Independent subclasses that derive from the same base.

Each subclass can use the base functionality or override it as needed.

Example

In this example, Bird and Fish inherit from the Animal class and override the move() method to define specific behaviors:

class Animal:
def move(self):
return "Moves in some way"
class Bird(Animal):
def move(self):
return "Flies"
class Fish(Animal):
def move(self):
return "Swims"
b = Bird()
f = Fish()
print(b.move())
print(f.move())

The output of this code is:

Flies
Swims

In this code:

  • Bird and Fish both inherit from the Animal class.
  • The move() method is overridden in each subclass to define movement specific to that type of animal.
  • This demonstrates how each child can provide its own interpretation of a shared method.

An illustration of how hierarchical inheritance is structured:

+-------------+
| Animal |
|-------------|
| move() |
+------+------+
/ \
▼ ▼
+-------------+ +-------------+
| Bird | | Fish |
|-------------| |-------------|
| move() | | move() |
+-------------+ +-------------+

Codebyte Example

In this example, Laptop and Desktop inherit shared functionality from the Device class while adding their own unique methods:

Code
Output

Benefits of Hierarchical Inheritance

  • Efficient reuse: Shared logic is centralized in the parent class.
  • Cleaner structure: Each subclass can focus on its unique behavior.
  • Expandable design: New types can be introduced without modifying existing code.

Choose hierarchical inheritance when multiple types share standard features but diverge in implementation.

All contributors

Contribute to Docs

Learn Python on Codecademy

  • Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!
    • Includes 6 Courses
    • With Professional Certification
    • Beginner Friendly.
      75 hours
  • Learn the basics of Python 3.12, one of the most powerful, versatile, and in-demand programming languages today.
    • With Certificate
    • Beginner Friendly.
      24 hours