Skip to content

docs: Add new term entry for Hierarchical Inheritance in Python #7107

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
---
Title: 'Hierarchical Inheritance'
Description: 'Explains hierarchical inheritance in Python, where multiple subclasses inherit from a single parent class.'
Subjects:
- 'Python'
- 'Object-Oriented Programming'
Tags:
- 'Classes'
- 'Inheritance'
- 'OOP'
CatalogContent:
- 'learn-python-3'
- 'paths/computer-science'
---

**Hierarchical inheritance** refers to a structure in object-oriented programming (OOP) where several subclasses share a common parent class. Each of these child classes gains access to the attributes and methods of the same base class, while also having the flexibility to define their own unique behavior.

This pattern encourages consistency and reduces redundancy in code—especially useful when multiple types share core functionality but differ in specialized behavior.

## Syntax

```python
class Parent:
# Base class with shared behavior

class ChildA(Parent):
# Inherits from Parent

class ChildB(Parent):
# Also inherits from Parent
```

- `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

```python
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()) # Output: Flies
print(f.move()) # Output: Swims
```

### Explanation

- `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.

## Codebyte Example

```codebyte/python
class Device:
def power(self):
return "Powered on"

class Laptop(Device):
def feature(self):
return "Portable computing"

class Desktop(Device):
def feature(self):
return "High-performance workstation"

l = Laptop()
d = Desktop()

print(l.power()) # Inherited from Device
print(l.feature()) # Specific to Laptop
print(d.power()) # Inherited from Device
print(d.feature()) # Specific to Desktop
```

## Diagram

An illustration of how hierarchical inheritance is structured:

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

### 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 common features but diverge in implementation.

### Related Concepts

* [Single Inheritance](../single-inheritance/single-inheritance.md)
* [Multiple Inheritance](../multiple-inheritance/multiple-inheritance.md)
* [Multilevel Inheritance](../multilevel-inheritance/multilevel-inheritance.md)
* [super() Function in Python](../../../built-in-functions/terms/super/super.md)