Skip to content

Commit 66c3c82

Browse files
committed
Add term entries for Multilevel Inheritance, Multiple Inheritance, and Hierarchical Inheritance in Python
1 parent 349ee28 commit 66c3c82

File tree

3 files changed

+360
-0
lines changed

3 files changed

+360
-0
lines changed
Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
---
2+
Title: 'Hierarchical Inheritance'
3+
Description: 'Explains hierarchical inheritance in Python, where multiple subclasses inherit from a single parent class.'
4+
Subjects:
5+
- 'Python'
6+
- 'Object-Oriented Programming'
7+
Tags:
8+
- 'Classes'
9+
- 'Inheritance'
10+
- 'OOP'
11+
CatalogContent:
12+
- 'learn-python-3'
13+
- 'paths/computer-science'
14+
---
15+
16+
**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.
17+
18+
This pattern encourages consistency and reduces redundancy in code—especially useful when multiple types share core functionality but differ in specialized behavior.
19+
20+
## Syntax
21+
22+
```python
23+
class Parent:
24+
# Base class with shared behavior
25+
26+
class ChildA(Parent):
27+
# Inherits from Parent
28+
29+
class ChildB(Parent):
30+
# Also inherits from Parent
31+
```
32+
33+
- `Parent`: The superclass that defines common functionality.
34+
- `ChildA` and `ChildB`: Independent subclasses that derive from the same base.
35+
36+
Each subclass can use the base functionality or override it as needed.
37+
38+
## Example
39+
40+
```python
41+
class Animal:
42+
def move(self):
43+
return "Moves in some way"
44+
45+
class Bird(Animal):
46+
def move(self):
47+
return "Flies"
48+
49+
class Fish(Animal):
50+
def move(self):
51+
return "Swims"
52+
53+
b = Bird()
54+
f = Fish()
55+
56+
print(b.move()) # Output: Flies
57+
print(f.move()) # Output: Swims
58+
```
59+
60+
### Explanation
61+
62+
- `Bird` and `Fish` both inherit from the `Animal` class.
63+
- The `move()` method is overridden in each subclass to define movement specific to that type of animal.
64+
- This demonstrates how each child can provide its own interpretation of a shared method.
65+
66+
## Codebyte Example
67+
68+
```codebyte/python
69+
class Device:
70+
def power(self):
71+
return "Powered on"
72+
73+
class Laptop(Device):
74+
def feature(self):
75+
return "Portable computing"
76+
77+
class Desktop(Device):
78+
def feature(self):
79+
return "High-performance workstation"
80+
81+
l = Laptop()
82+
d = Desktop()
83+
84+
print(l.power()) # Inherited from Device
85+
print(l.feature()) # Specific to Laptop
86+
print(d.power()) # Inherited from Device
87+
print(d.feature()) # Specific to Desktop
88+
```
89+
90+
## Diagram
91+
92+
An illustration of how hierarchical inheritance is structured:
93+
94+
```
95+
+-------------+
96+
| Animal |
97+
|-------------|
98+
| move() |
99+
+------+------+
100+
/ \
101+
▼ ▼
102+
+-------------+ +-------------+
103+
| Bird | | Fish |
104+
|-------------| |-------------|
105+
| move() | | move() |
106+
+-------------+ +-------------+
107+
```
108+
109+
### Benefits of Hierarchical Inheritance
110+
111+
- **Efficient reuse**: Shared logic is centralized in the parent class.
112+
- **Cleaner structure**: Each subclass can focus on its unique behavior.
113+
- **Expandable design**: New types can be introduced without modifying existing code.
114+
115+
> Choose hierarchical inheritance when multiple types share common features but diverge in implementation.
116+
117+
### Related Concepts
118+
119+
* [Single Inheritance](../single-inheritance/single-inheritance.md)
120+
* [Multiple Inheritance](../multiple-inheritance/multiple-inheritance.md)
121+
* [Multilevel Inheritance](../multilevel-inheritance/multilevel-inheritance.md)
122+
* [super() Function in Python](../../../built-in-functions/terms/super/super.md)
Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
---
2+
Title: 'Multilevel Inheritance'
3+
Description: 'Explains multilevel inheritance in Python, where a class inherits from a derived class forming a chain of inheritance.'
4+
Subjects:
5+
- 'Python'
6+
- 'Object-Oriented Programming'
7+
Tags:
8+
- 'Classes'
9+
- 'Inheritance'
10+
- 'OOP'
11+
CatalogContent:
12+
- 'learn-python-3'
13+
- 'paths/computer-science'
14+
---
15+
16+
**Multilevel inheritance** is a type of class hierarchy in object-oriented programming (OOP) where a class inherits from a derived class, resulting in a layered inheritance chain. Each level passes its functionality to the next, enabling cumulative extension and specialization.
17+
18+
In Python, multilevel inheritance allows a subclass to indirectly inherit members from a grandparent class through its parent.
19+
20+
## Syntax
21+
22+
```python
23+
class BaseClass:
24+
# Base class code
25+
26+
class IntermediateClass(BaseClass):
27+
# Inherits from BaseClass
28+
29+
class DerivedClass(IntermediateClass):
30+
# Inherits from IntermediateClass
31+
```
32+
33+
- `BaseClass`: The top-most class in the hierarchy.
34+
- `IntermediateClass`: Inherits from `BaseClass`.
35+
- `DerivedClass`: Inherits from `IntermediateClass`, and indirectly from `BaseClass`.
36+
37+
## Example
38+
39+
```python
40+
class LivingBeing:
41+
def breathe(self):
42+
return "Breathing"
43+
44+
class Animal(LivingBeing):
45+
def move(self):
46+
return "Moving"
47+
48+
class Dog(Animal):
49+
def bark(self):
50+
return "Barking"
51+
52+
dog = Dog()
53+
print(dog.breathe()) # Inherited from LivingBeing
54+
print(dog.move()) # Inherited from Animal
55+
print(dog.bark()) # Defined in Dog
56+
```
57+
58+
### Explanation:
59+
60+
This example forms a chain:
61+
62+
- `Dog``Animal``LivingBeing`
63+
- The `Dog` class gains all the methods of its parent and grandparent.
64+
65+
## Codebyte Example
66+
67+
```codebyte/python
68+
class Device:
69+
def power_on(self):
70+
return "Device powered on"
71+
72+
class Computer(Device):
73+
def open_os(self):
74+
return "OS is loading"
75+
76+
class Laptop(Computer):
77+
def use_touchpad(self):
78+
return "Using touchpad"
79+
80+
my_laptop = Laptop()
81+
82+
print(my_laptop.power_on())
83+
print(my_laptop.open_os())
84+
print(my_laptop.use_touchpad())
85+
```
86+
87+
## Diagram
88+
89+
```
90+
+--------------+
91+
| LivingBeing |
92+
|--------------|
93+
| breathe() |
94+
+------+-------+
95+
|
96+
97+
+--------------+
98+
| Animal |
99+
|--------------|
100+
| move() |
101+
+------+-------+
102+
|
103+
104+
+--------------+
105+
| Dog |
106+
|--------------|
107+
| bark() |
108+
+--------------+
109+
```
110+
111+
### Key Takeaways
112+
113+
- Promotes structured layering of functionality.
114+
- Each derived class builds upon its predecessor.
115+
- Best used when deeper specialization is logically needed.
116+
117+
> Avoid overuse, as deep inheritance hierarchies can make debugging and comprehension harder.
118+
119+
### Related Concepts
120+
121+
* [Single Inheritance](../single-inheritance/single-inheritance.md)
122+
* [Multiple Inheritance](../multiple-inheritance/multiple-inheritance.md)
123+
* [super() Function in Python](../../../built-in-functions/terms/super/super.md)
Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
---
2+
Title: 'Multiple Inheritance'
3+
Description: 'Explains multiple inheritance in Python, where a subclass inherits from more than one parent class.'
4+
Subjects:
5+
- 'Python'
6+
- 'Object-Oriented Programming'
7+
Tags:
8+
- 'Classes'
9+
- 'Inheritance'
10+
- 'OOP'
11+
CatalogContent:
12+
- 'learn-python-3'
13+
- 'paths/computer-science'
14+
---
15+
16+
**Multiple inheritance** is a concept in object-oriented programming (OOP) where a single class can derive functionality from more than one parent class. Python allows this, making it possible for a subclass to inherit features from multiple independent base classes.
17+
18+
This design encourages flexibility and reusability of code. However, it also introduces ambiguity when parent classes define methods or properties with the same name. Python addresses such conflicts using its **Method Resolution Order (MRO)**.
19+
20+
## Syntax
21+
22+
```python
23+
class ParentA:
24+
# ParentA methods and attributes
25+
26+
class ParentB:
27+
# ParentB methods and attributes
28+
29+
class Child(ParentA, ParentB):
30+
# Inherits from both ParentA and ParentB
31+
```
32+
33+
- `ParentA`, `ParentB`: Two distinct base classes.
34+
- `Child`: The derived class that inherits from both.
35+
- Python determines which method to use using the order declared (left to right).
36+
37+
## Example
38+
39+
```python
40+
class Flyer:
41+
def ability(self):
42+
return "Can fly"
43+
44+
class Swimmer:
45+
def ability(self):
46+
return "Can swim"
47+
48+
class Duck(Flyer, Swimmer):
49+
pass
50+
51+
d = Duck()
52+
print(d.ability()) # Output: Can fly
53+
```
54+
55+
### Explanation:
56+
57+
- `Duck` inherits from both `Flyer` and `Swimmer`.
58+
- Since both define the same method, Python uses the version from `Flyer` because it appears first.
59+
- The method selection follows the **MRO** rules.
60+
61+
## Codebyte Example
62+
63+
```codebyte/python
64+
class Keyboard:
65+
def input_method(self):
66+
return "Typed input"
67+
68+
class Touchscreen:
69+
def input_method(self):
70+
return "Touch input"
71+
72+
class Tablet(Keyboard, Touchscreen):
73+
def description(self):
74+
return "Tablet can accept multiple input types"
75+
76+
my_tablet = Tablet()
77+
78+
print(my_tablet.input_method()) # Inherited from Keyboard (first in order)
79+
print(my_tablet.description()) # Defined in Tablet
80+
```
81+
82+
## Diagram
83+
84+
A visual example of multiple inheritance:
85+
86+
```
87+
+-------------+ +---------------+
88+
| Keyboard | | Touchscreen |
89+
|-------------| |---------------|
90+
| input_method| | input_method |
91+
+------+------+ +-------+-------+
92+
\ /
93+
\ /
94+
▼ ▼
95+
+--------------------+
96+
| Tablet |
97+
|--------------------|
98+
| description() |
99+
+--------------------+
100+
```
101+
102+
### Key Considerations
103+
104+
- Encourages combining capabilities from separate classes.
105+
- Ideal for mixins—lightweight, reusable functionality.
106+
- Be aware of method name clashes and resolution order.
107+
- Python provides tools like `super()` and `.mro()` to navigate complex inheritance trees.
108+
109+
> Choose multiple inheritance when your subclass needs to aggregate unrelated behaviors—just make sure method conflicts are intentionally handled.
110+
111+
### Related Concepts
112+
113+
* [Single Inheritance](../single-inheritance/single-inheritance.md)
114+
* [Multilevel Inheritance](../multilevel-inheritance/multilevel-inheritance.md)
115+
* [super() Function in Python](../../../built-in-functions/terms/super/super.md)

0 commit comments

Comments
 (0)