Skip to content

Commit 936945e

Browse files
committed
docs: add term entry for Single Inheritance in Python
This commit adds a new term entry under `content/python/concepts/inheritance/terms/single-inheritance/`. It includes: - Metadata and SEO-optimized description - Definition with clear examples - Syntax section - Visual diagram for understanding - A runnable Codebyte in Python - Cross-links to related concepts Closes #7078
1 parent 6779331 commit 936945e

File tree

1 file changed

+106
-0
lines changed

1 file changed

+106
-0
lines changed
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
---
2+
Title: 'Single Inheritance'
3+
Description: 'Explains single inheritance in Python, where one subclass inherits 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+
**Single inheritance** is a foundational principle in object-oriented programming (OOP), where a class (known as the child or subclass) derives its behaviour and structure from one parent class (or superclass). This mechanism promotes code reusability and clear hierarchical design.
17+
18+
In Python, single inheritance is implemented by defining the subclass with the parent class name in parentheses. This allows the child class to use, override, or extend the functionality of its parent.
19+
20+
## Syntax
21+
22+
```python
23+
class ParentClass:
24+
# Methods and attributes of the parent class
25+
26+
class ChildClass(ParentClass):
27+
# Inherits from ParentClass
28+
# Can override or add new functionality
29+
```
30+
31+
- `ParentClass`: The class being inherited from.
32+
- `ChildClass`: The subclass that inherits behaviour.
33+
- The child class gains access to the parent’s members and can override or expand upon them.
34+
35+
## Example
36+
37+
```python
38+
class Animal:
39+
def speak(self):
40+
return "Makes a sound"
41+
42+
class Dog(Animal):
43+
def speak(self):
44+
return "Barks"
45+
46+
a = Animal()
47+
d = Dog()
48+
49+
print(a.speak()) # Output: Makes a sound
50+
print(d.speak()) # Output: Barks
51+
```
52+
53+
### Explanation
54+
55+
- `Dog` inherits from the `Animal` class.
56+
- Both define a `speak()` method, but `Dog` overrides it to provide a more specific output.
57+
- This demonstrates how subclasses can customise inherited behaviour.
58+
59+
## Codebyte Example
60+
61+
```codebyte/python
62+
class Vehicle:
63+
def start_engine(self):
64+
return "Engine started"
65+
66+
class Car(Vehicle):
67+
def drive(self):
68+
return "Car is driving"
69+
70+
my_car = Car()
71+
72+
print(my_car.start_engine()) # Inherited from Vehicle
73+
print(my_car.drive()) # Defined in Car
74+
```
75+
76+
## Diagram
77+
78+
Visual structure of single inheritance:
79+
80+
```
81+
+-------------+
82+
| Vehicle |
83+
|-------------|
84+
| start_engine|
85+
+------+------+
86+
|
87+
88+
+-------------+
89+
| Car |
90+
|-------------|
91+
| drive |
92+
+-------------+
93+
```
94+
95+
### Advantages of Single Inheritance
96+
97+
- **Reusability**: Shared logic lives in the parent class and can be used across multiple child classes.
98+
- **Modularity**: Logical separation of generic and specific behaviours.
99+
- **Simplicity**: Easier to trace inheritance paths and maintain codebases.
100+
101+
> Use single inheritance when the subclass is a specialised form of the parent and there’s no need to inherit from multiple sources.
102+
103+
### Related Concepts
104+
105+
* [Multiple Inheritance](../../inheritance.md)
106+
* [super() Function in Python](../../../built-in-functions/terms/super/super.md)

0 commit comments

Comments
 (0)