From 936945ed851528530090fa2d7c5d4f13700d1ad1 Mon Sep 17 00:00:00 2001 From: Learner <92308896+Prankisster@users.noreply.github.com> Date: Thu, 12 Jun 2025 17:50:20 +0530 Subject: [PATCH 1/4] 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 --- .../single-inheritance/single-inheritance.md | 106 ++++++++++++++++++ 1 file changed, 106 insertions(+) create mode 100644 content/python/concepts/inheritance/terms/single-inheritance/single-inheritance.md diff --git a/content/python/concepts/inheritance/terms/single-inheritance/single-inheritance.md b/content/python/concepts/inheritance/terms/single-inheritance/single-inheritance.md new file mode 100644 index 00000000000..f7f0aa0578a --- /dev/null +++ b/content/python/concepts/inheritance/terms/single-inheritance/single-inheritance.md @@ -0,0 +1,106 @@ +--- +Title: 'Single Inheritance' +Description: 'Explains single inheritance in Python, where one subclass inherits from a single parent class.' +Subjects: + - 'Python' + - 'Object-Oriented Programming' +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 (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. + +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. + +## Syntax + +```python +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. +- `ChildClass`: The subclass that inherits behaviour. +- The child class gains access to the parent’s members and can override or expand upon them. + +## Example + +```python +class Animal: + def speak(self): + return "Makes a sound" + +class Dog(Animal): + def speak(self): + return "Barks" + +a = Animal() +d = Dog() + +print(a.speak()) # Output: Makes a sound +print(d.speak()) # Output: Barks +``` + +### 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 Example + +```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()) # Inherited from Vehicle +print(my_car.drive()) # Defined in Car +``` + +## Diagram + +Visual structure of single inheritance: + +``` ++-------------+ +| Vehicle | +|-------------| +| start_engine| ++------+------+ + | + ▼ ++-------------+ +| Car | +|-------------| +| drive | ++-------------+ +``` + +### 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. + +### Related Concepts + +* [Multiple Inheritance](../../inheritance.md) +* [super() Function in Python](../../../built-in-functions/terms/super/super.md) From 3bcd9618a7dc17eff2b9b6f3a2b114916f57da92 Mon Sep 17 00:00:00 2001 From: Learner <92308896+Prankisster@users.noreply.github.com> Date: Mon, 16 Jun 2025 12:48:56 +0530 Subject: [PATCH 2/4] updated with requested changes --- .../single-inheritance/single-inheritance.md | 43 ++++++++++--------- 1 file changed, 23 insertions(+), 20 deletions(-) diff --git a/content/python/concepts/inheritance/terms/single-inheritance/single-inheritance.md b/content/python/concepts/inheritance/terms/single-inheritance/single-inheritance.md index f7f0aa0578a..429356c3dd2 100644 --- a/content/python/concepts/inheritance/terms/single-inheritance/single-inheritance.md +++ b/content/python/concepts/inheritance/terms/single-inheritance/single-inheritance.md @@ -1,9 +1,9 @@ --- Title: 'Single Inheritance' -Description: 'Explains single inheritance in Python, where one subclass inherits from a single parent class.' +Description: 'Single inheritance allows a class to derive behavior and attributes from one parent class only.' Subjects: - - 'Python' - - 'Object-Oriented Programming' + - 'Code Foundations' + - 'Computer Science' Tags: - 'Classes' - 'Inheritance' @@ -13,13 +13,13 @@ CatalogContent: - 'paths/computer-science' --- -**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. +**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 allows the child class to use, override, or extend the functionality of its parent. +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 -```python +```pseudo class ParentClass: # Methods and attributes of the parent class @@ -28,13 +28,16 @@ class ChildClass(ParentClass): # Can override or add new functionality ``` -- `ParentClass`: The class being inherited from. -- `ChildClass`: The subclass that inherits behaviour. -- The child class gains access to the parent’s members and can override or expand upon them. +- `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 -```python +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" @@ -46,8 +49,14 @@ class Dog(Animal): a = Animal() d = Dog() -print(a.speak()) # Output: Makes a sound -print(d.speak()) # Output: Barks +print(a.speak()) +print(d.speak()) +``` +**Output** + +```Sh +Makes a sound +Barks ``` ### Explanation @@ -56,7 +65,6 @@ print(d.speak()) # Output: Barks - Both define a `speak()` method, but `Dog` overrides it to provide a more specific output. - This demonstrates how subclasses can customise inherited behaviour. -## Codebyte Example ```codebyte/python class Vehicle: @@ -69,8 +77,8 @@ class Car(Vehicle): my_car = Car() -print(my_car.start_engine()) # Inherited from Vehicle -print(my_car.drive()) # Defined in Car +print(my_car.start_engine()) +print(my_car.drive()) ``` ## Diagram @@ -99,8 +107,3 @@ Visual structure of single inheritance: - **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. - -### Related Concepts - -* [Multiple Inheritance](../../inheritance.md) -* [super() Function in Python](../../../built-in-functions/terms/super/super.md) From 287b80a85332d4536c3339f0aae39f2df4540cd5 Mon Sep 17 00:00:00 2001 From: Daksha Deep Date: Sun, 22 Jun 2025 19:51:23 +0530 Subject: [PATCH 3/4] fixing indentation --- .../single-inheritance/single-inheritance.md | 31 ++++++++++--------- 1 file changed, 16 insertions(+), 15 deletions(-) diff --git a/content/python/concepts/inheritance/terms/single-inheritance/single-inheritance.md b/content/python/concepts/inheritance/terms/single-inheritance/single-inheritance.md index 429356c3dd2..ab928d9e01c 100644 --- a/content/python/concepts/inheritance/terms/single-inheritance/single-inheritance.md +++ b/content/python/concepts/inheritance/terms/single-inheritance/single-inheritance.md @@ -13,7 +13,7 @@ CatalogContent: - '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. +**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. 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. @@ -21,14 +21,14 @@ In Python, single inheritance is implemented by defining the subclass with the p ```pseudo class ParentClass: - # Methods and attributes of the parent class + # Methods and attributes of the parent class class ChildClass(ParentClass): - # Inherits from ParentClass - # Can override or add new functionality + # Inherits from ParentClass + # Can override or add new functionality ``` -- `ParentClass`: The class being inherited from (also known as superclass or base class). +- `ParentClass`: The class 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. @@ -39,12 +39,12 @@ This example demonstrates single inheritance where the `Dog` class inherits from ```py class Animal: - def speak(self): - return "Makes a sound" + def speak(self): + return "Makes a sound" class Dog(Animal): - def speak(self): - return "Barks" + def speak(self): + return "Barks" a = Animal() d = Dog() @@ -52,9 +52,10 @@ d = Dog() print(a.speak()) print(d.speak()) ``` -**Output** -```Sh +Here's the output: + +```shell Makes a sound Barks ``` @@ -68,12 +69,12 @@ Barks ```codebyte/python class Vehicle: - def start_engine(self): - return "Engine started" + def start_engine(self): + return "Engine started" class Car(Vehicle): - def drive(self): - return "Car is driving" + def drive(self): + return "Car is driving" my_car = Car() From 162dbb4eebdcaf90d1eb95f286f7ffc7b3fb247f Mon Sep 17 00:00:00 2001 From: Daksha Deep Date: Sun, 22 Jun 2025 19:51:57 +0530 Subject: [PATCH 4/4] minor changes --- .../inheritance/terms/single-inheritance/single-inheritance.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/content/python/concepts/inheritance/terms/single-inheritance/single-inheritance.md b/content/python/concepts/inheritance/terms/single-inheritance/single-inheritance.md index ab928d9e01c..d80261de901 100644 --- a/content/python/concepts/inheritance/terms/single-inheritance/single-inheritance.md +++ b/content/python/concepts/inheritance/terms/single-inheritance/single-inheritance.md @@ -31,7 +31,7 @@ class ChildClass(ParentClass): - `ParentClass`: The class 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. +The `ParentClass` is passed inside parentheses when defining the `ChildClass`. ## Example