Skip to content

Commit 1f16320

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 1f16320

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 core concept in object-oriented programming (OOP), where a class (called the child or subclass) inherits attributes and methods from one parent (or superclass). It enables code reuse, logical hierarchy, and cleaner structure in simple inheritance relationships.
17+
18+
In Python, single inheritance is achieved by defining the subclass with the name of the superclass in parentheses. This allows the subclass to extend or override the functionality of the parent class.
19+
20+
## Syntax
21+
22+
```python
23+
class ParentClass:
24+
# Parent class attributes and methods
25+
26+
class ChildClass(ParentClass):
27+
# Child class inherits from ParentClass
28+
# Can override or extend functionality
29+
````
30+
31+
* `ParentClass`: The base class whose methods and attributes are inherited.
32+
* `ChildClass`: The derived class that inherits from `ParentClass`.
33+
* The subclass can override methods from the parent or introduce its own new behavior.
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 `Animal`.
56+
* Both classes define a `speak()` method, but `Dog` overrides the inherited method.
57+
* This shows how subclasses can provide specialized behavior while still inheriting from the parent.
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 method
73+
print(my_car.drive()) # Subclass method
74+
```
75+
76+
## Diagram
77+
78+
Here's a simple representation 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+
By using single inheritance:
96+
97+
* **Code reusability** is enhanced.
98+
* **Modularity** improves through separation of concerns.
99+
* **Maintenance** becomes easier since shared behavior is centralized.
100+
101+
> ⚠️ Use single inheritance when a subclass logically "is-a" type of its superclass and no multiple base classes are needed.
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)