-
Notifications
You must be signed in to change notification settings - Fork 4.1k
Add term entry for Single Inheritance in Python #7096
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
Prankisster
wants to merge
3
commits into
Codecademy:main
Choose a base branch
from
Prankisster:add/single-inheritance-python-term
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
109 changes: 109 additions & 0 deletions
109
content/python/concepts/inheritance/terms/single-inheritance/single-inheritance.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
--- | ||
Title: 'Single Inheritance' | ||
Description: 'Single inheritance allows a class to derive behavior and attributes from one parent class only.' | ||
Subjects: | ||
- 'Code Foundations' | ||
- 'Computer Science' | ||
Tags: | ||
- 'Classes' | ||
- 'Inheritance' | ||
- 'OOP' | ||
CatalogContent: | ||
- 'learn-python-3' | ||
- 'paths/computer-science' | ||
--- | ||
|
||
**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 clear maintains a clear logical hierarchicy. | ||
|
||
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. | ||
|
||
## Syntax | ||
|
||
```pseudo | ||
class ParentClass: | ||
# Methods and attributes of the parent class | ||
|
||
class ChildClass(ParentClass): | ||
# Inherits from ParentClass | ||
# Can override or add new functionality | ||
``` | ||
|
||
- `ParentClass`: The class being inherited from (also known as superclass or base class). | ||
- `ChildClass`: The class that inherits from the parent class (also known as the subclass or the derived class). | ||
|
||
The ParentClass is passed inside parentheses when defining the ChildClass. | ||
|
||
## Example | ||
|
||
Prankisster marked this conversation as resolved.
Show resolved
Hide resolved
|
||
This example demonstrates single inheritance where the `Dog` class inherits from `Animal` and overrides the `speak()` method to customize behavior: | ||
|
||
```py | ||
class Animal: | ||
def speak(self): | ||
return "Makes a sound" | ||
|
||
class Dog(Animal): | ||
def speak(self): | ||
return "Barks" | ||
|
||
a = Animal() | ||
d = Dog() | ||
|
||
print(a.speak()) | ||
print(d.speak()) | ||
``` | ||
**Output** | ||
|
||
```Sh | ||
Makes a sound | ||
Barks | ||
``` | ||
|
||
Prankisster marked this conversation as resolved.
Show resolved
Hide resolved
|
||
### Explanation | ||
|
||
- `Dog` inherits from the `Animal` class. | ||
- Both define a `speak()` method, but `Dog` overrides it to provide a more specific output. | ||
- This demonstrates how subclasses can customise inherited behaviour. | ||
|
||
|
||
```codebyte/python | ||
class Vehicle: | ||
def start_engine(self): | ||
return "Engine started" | ||
|
||
class Car(Vehicle): | ||
def drive(self): | ||
return "Car is driving" | ||
|
||
my_car = Car() | ||
|
||
print(my_car.start_engine()) | ||
print(my_car.drive()) | ||
``` | ||
|
||
## Diagram | ||
|
||
Visual structure of single inheritance: | ||
|
||
``` | ||
+-------------+ | ||
| Vehicle | | ||
|-------------| | ||
| start_engine| | ||
+------+------+ | ||
| | ||
▼ | ||
+-------------+ | ||
| Car | | ||
|-------------| | ||
| drive | | ||
+-------------+ | ||
Comment on lines
+86
to
+100
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can we have a pictorial represenatation of single inheritance here? |
||
``` | ||
|
||
### Advantages of Single Inheritance | ||
|
||
- **Reusability**: Shared logic lives in the parent class and can be used across multiple child classes. | ||
- **Modularity**: Logical separation of generic and specific behaviours. | ||
- **Simplicity**: Easier to trace inheritance paths and maintain codebases. | ||
|
||
> Use single inheritance when the subclass is a specialised form of the parent and there’s no need to inherit from multiple sources. |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.