Skip to content

Commit e021ac9

Browse files
authored
Add term entry for Single Inheritance in Python (#7096)
* 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 * updated with requested changes * fixing indentation * minor changes * format fix ---------
1 parent 97638c5 commit e021ac9

File tree

1 file changed

+109
-0
lines changed

1 file changed

+109
-0
lines changed
Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
---
2+
Title: 'Single Inheritance'
3+
Description: 'Single inheritance allows a class to derive behavior and attributes from one parent class only.'
4+
Subjects:
5+
- 'Code Foundations'
6+
- 'Computer Science'
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 (called the child or subclass) inherits its behaviour and structure from a single parent class (or superclass). This promotes code reusability and maintains a clear logical hierarchy.
17+
18+
In Python, single inheritance is implemented by defining the subclass with the parent class name in parentheses. This enables the child class to use, override, or extend the methods and attributes of its parent.
19+
20+
## Syntax
21+
22+
```pseudo
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 inherited from (also known as superclass or base class).
32+
- `ChildClass`: The class that inherits from the parent class (also known as the subclass or the derived class).
33+
34+
The `ParentClass` is passed inside parentheses when defining the `ChildClass`.
35+
36+
## Example
37+
38+
This example demonstrates single inheritance where the `Dog` class inherits from `Animal` and overrides the `speak()` method to customize behavior:
39+
40+
```py
41+
class Animal:
42+
def speak(self):
43+
return "Makes a sound"
44+
45+
class Dog(Animal):
46+
def speak(self):
47+
return "Barks"
48+
49+
a = Animal()
50+
d = Dog()
51+
52+
print(a.speak())
53+
print(d.speak())
54+
```
55+
56+
Here's the output:
57+
58+
```shell
59+
Makes a sound
60+
Barks
61+
```
62+
63+
### Explanation
64+
65+
- `Dog` inherits from the `Animal` class.
66+
- Both define a `speak()` method, but `Dog` overrides it to provide a more specific output.
67+
- This demonstrates how subclasses can customise inherited behaviour.
68+
69+
```codebyte/python
70+
class Vehicle:
71+
def start_engine(self):
72+
return "Engine started"
73+
74+
class Car(Vehicle):
75+
def drive(self):
76+
return "Car is driving"
77+
78+
my_car = Car()
79+
80+
print(my_car.start_engine())
81+
print(my_car.drive())
82+
```
83+
84+
## Diagram
85+
86+
Visual structure of single inheritance:
87+
88+
```
89+
+-------------+
90+
| Vehicle |
91+
|-------------|
92+
| start_engine|
93+
+------+------+
94+
|
95+
96+
+-------------+
97+
| Car |
98+
|-------------|
99+
| drive |
100+
+-------------+
101+
```
102+
103+
### Advantages of Single Inheritance
104+
105+
- **Reusability**: Shared logic lives in the parent class and can be used across multiple child classes.
106+
- **Modularity**: Logical separation of generic and specific behaviours.
107+
- **Simplicity**: Easier to trace inheritance paths and maintain codebases.
108+
109+
> Use single inheritance when the subclass is a specialised form of the parent and there’s no need to inherit from multiple sources.

0 commit comments

Comments
 (0)