diff --git a/content/python/concepts/inheritance/terms/hierarchical-inheritance/hierarchical-inheritance.md b/content/python/concepts/inheritance/terms/hierarchical-inheritance/hierarchical-inheritance.md new file mode 100644 index 00000000000..d6913a9a933 --- /dev/null +++ b/content/python/concepts/inheritance/terms/hierarchical-inheritance/hierarchical-inheritance.md @@ -0,0 +1,122 @@ +--- +Title: 'Hierarchical Inheritance' +Description: 'Explains hierarchical inheritance in Python, where multiple subclasses inherit from a single parent class.' +Subjects: + - 'Python' + - 'Object-Oriented Programming' +Tags: + - 'Classes' + - 'Inheritance' + - 'OOP' +CatalogContent: + - 'learn-python-3' + - 'paths/computer-science' +--- + +**Hierarchical inheritance** refers to a structure in object-oriented programming (OOP) where several subclasses share a common parent class. Each of these child classes gains access to the attributes and methods of the same base class, while also having the flexibility to define their own unique behavior. + +This pattern encourages consistency and reduces redundancy in code—especially useful when multiple types share core functionality but differ in specialized behavior. + +## Syntax + +```python +class Parent: + # Base class with shared behavior + +class ChildA(Parent): + # Inherits from Parent + +class ChildB(Parent): + # Also inherits from Parent +``` + +- `Parent`: The superclass that defines common functionality. +- `ChildA` and `ChildB`: Independent subclasses that derive from the same base. + +Each subclass can use the base functionality or override it as needed. + +## Example + +```python +class Animal: + def move(self): + return "Moves in some way" + +class Bird(Animal): + def move(self): + return "Flies" + +class Fish(Animal): + def move(self): + return "Swims" + +b = Bird() +f = Fish() + +print(b.move()) # Output: Flies +print(f.move()) # Output: Swims +``` + +### Explanation + +- `Bird` and `Fish` both inherit from the `Animal` class. +- The `move()` method is overridden in each subclass to define movement specific to that type of animal. +- This demonstrates how each child can provide its own interpretation of a shared method. + +## Codebyte Example + +```codebyte/python +class Device: + def power(self): + return "Powered on" + +class Laptop(Device): + def feature(self): + return "Portable computing" + +class Desktop(Device): + def feature(self): + return "High-performance workstation" + +l = Laptop() +d = Desktop() + +print(l.power()) # Inherited from Device +print(l.feature()) # Specific to Laptop +print(d.power()) # Inherited from Device +print(d.feature()) # Specific to Desktop +``` + +## Diagram + +An illustration of how hierarchical inheritance is structured: + +``` + +-------------+ + | Animal | + |-------------| + | move() | + +------+------+ + / \ + ▼ ▼ ++-------------+ +-------------+ +| Bird | | Fish | +|-------------| |-------------| +| move() | | move() | ++-------------+ +-------------+ +``` + +### Benefits of Hierarchical Inheritance + +- **Efficient reuse**: Shared logic is centralized in the parent class. +- **Cleaner structure**: Each subclass can focus on its unique behavior. +- **Expandable design**: New types can be introduced without modifying existing code. + +> Choose hierarchical inheritance when multiple types share common features but diverge in implementation. + +### Related Concepts + +* [Single Inheritance](../single-inheritance/single-inheritance.md) +* [Multiple Inheritance](../multiple-inheritance/multiple-inheritance.md) +* [Multilevel Inheritance](../multilevel-inheritance/multilevel-inheritance.md) +* [super() Function in Python](../../../built-in-functions/terms/super/super.md) diff --git a/content/python/concepts/inheritance/terms/multilevel-inheritance/multilevel-inheritance.md b/content/python/concepts/inheritance/terms/multilevel-inheritance/multilevel-inheritance.md new file mode 100644 index 00000000000..235bc908bb3 --- /dev/null +++ b/content/python/concepts/inheritance/terms/multilevel-inheritance/multilevel-inheritance.md @@ -0,0 +1,123 @@ +--- +Title: 'Multilevel Inheritance' +Description: 'Explains multilevel inheritance in Python, where a class inherits from a derived class forming a chain of inheritance.' +Subjects: + - 'Python' + - 'Object-Oriented Programming' +Tags: + - 'Classes' + - 'Inheritance' + - 'OOP' +CatalogContent: + - 'learn-python-3' + - 'paths/computer-science' +--- + +**Multilevel inheritance** is a type of class hierarchy in object-oriented programming (OOP) where a class inherits from a derived class, resulting in a layered inheritance chain. Each level passes its functionality to the next, enabling cumulative extension and specialization. + +In Python, multilevel inheritance allows a subclass to indirectly inherit members from a grandparent class through its parent. + +## Syntax + +```python +class BaseClass: + # Base class code + +class IntermediateClass(BaseClass): + # Inherits from BaseClass + +class DerivedClass(IntermediateClass): + # Inherits from IntermediateClass +``` + +- `BaseClass`: The top-most class in the hierarchy. +- `IntermediateClass`: Inherits from `BaseClass`. +- `DerivedClass`: Inherits from `IntermediateClass`, and indirectly from `BaseClass`. + +## Example + +```python +class LivingBeing: + def breathe(self): + return "Breathing" + +class Animal(LivingBeing): + def move(self): + return "Moving" + +class Dog(Animal): + def bark(self): + return "Barking" + +dog = Dog() +print(dog.breathe()) # Inherited from LivingBeing +print(dog.move()) # Inherited from Animal +print(dog.bark()) # Defined in Dog +``` + +### Explanation: + +This example forms a chain: + +- `Dog` → `Animal` → `LivingBeing` +- The `Dog` class gains all the methods of its parent and grandparent. + +## Codebyte Example + +```codebyte/python +class Device: + def power_on(self): + return "Device powered on" + +class Computer(Device): + def open_os(self): + return "OS is loading" + +class Laptop(Computer): + def use_touchpad(self): + return "Using touchpad" + +my_laptop = Laptop() + +print(my_laptop.power_on()) +print(my_laptop.open_os()) +print(my_laptop.use_touchpad()) +``` + +## Diagram + +``` ++--------------+ +| LivingBeing | +|--------------| +| breathe() | ++------+-------+ + | + ▼ ++--------------+ +| Animal | +|--------------| +| move() | ++------+-------+ + | + ▼ ++--------------+ +| Dog | +|--------------| +| bark() | ++--------------+ +``` + +### Key Takeaways + +- Promotes structured layering of functionality. +- Each derived class builds upon its predecessor. +- Best used when deeper specialization is logically needed. + +> Avoid overuse, as deep inheritance hierarchies can make debugging and comprehension harder. + +### Related Concepts + +* [Single Inheritance](../single-inheritance/single-inheritance.md) +* [Multiple Inheritance](../multiple-inheritance/multiple-inheritance.md) +* [super() Function in Python](../../../built-in-functions/terms/super/super.md) diff --git a/content/python/concepts/inheritance/terms/multiple-inheritance/multiple-inheritance.md b/content/python/concepts/inheritance/terms/multiple-inheritance/multiple-inheritance.md new file mode 100644 index 00000000000..fd3de6773f1 --- /dev/null +++ b/content/python/concepts/inheritance/terms/multiple-inheritance/multiple-inheritance.md @@ -0,0 +1,115 @@ +--- +Title: 'Multiple Inheritance' +Description: 'Explains multiple inheritance in Python, where a subclass inherits from more than one parent class.' +Subjects: + - 'Python' + - 'Object-Oriented Programming' +Tags: + - 'Classes' + - 'Inheritance' + - 'OOP' +CatalogContent: + - 'learn-python-3' + - 'paths/computer-science' +--- + +**Multiple inheritance** is a concept in object-oriented programming (OOP) where a single class can derive functionality from more than one parent class. Python allows this, making it possible for a subclass to inherit features from multiple independent base classes. + +This design encourages flexibility and reusability of code. However, it also introduces ambiguity when parent classes define methods or properties with the same name. Python addresses such conflicts using its **Method Resolution Order (MRO)**. + +## Syntax + +```python +class ParentA: + # ParentA methods and attributes + +class ParentB: + # ParentB methods and attributes + +class Child(ParentA, ParentB): + # Inherits from both ParentA and ParentB +``` + +- `ParentA`, `ParentB`: Two distinct base classes. +- `Child`: The derived class that inherits from both. +- Python determines which method to use using the order declared (left to right). + +## Example + +```python +class Flyer: + def ability(self): + return "Can fly" + +class Swimmer: + def ability(self): + return "Can swim" + +class Duck(Flyer, Swimmer): + pass + +d = Duck() +print(d.ability()) # Output: Can fly +``` + +### Explanation: + +- `Duck` inherits from both `Flyer` and `Swimmer`. +- Since both define the same method, Python uses the version from `Flyer` because it appears first. +- The method selection follows the **MRO** rules. + +## Codebyte Example + +```codebyte/python +class Keyboard: + def input_method(self): + return "Typed input" + +class Touchscreen: + def input_method(self): + return "Touch input" + +class Tablet(Keyboard, Touchscreen): + def description(self): + return "Tablet can accept multiple input types" + +my_tablet = Tablet() + +print(my_tablet.input_method()) # Inherited from Keyboard (first in order) +print(my_tablet.description()) # Defined in Tablet +``` + +## Diagram + +A visual example of multiple inheritance: + +``` ++-------------+ +---------------+ +| Keyboard | | Touchscreen | +|-------------| |---------------| +| input_method| | input_method | ++------+------+ +-------+-------+ + \ / + \ / + ▼ ▼ + +--------------------+ + | Tablet | + |--------------------| + | description() | + +--------------------+ +``` + +### Key Considerations + +- Encourages combining capabilities from separate classes. +- Ideal for mixins—lightweight, reusable functionality. +- Be aware of method name clashes and resolution order. +- Python provides tools like `super()` and `.mro()` to navigate complex inheritance trees. + +> Choose multiple inheritance when your subclass needs to aggregate unrelated behaviors—just make sure method conflicts are intentionally handled. + +### Related Concepts + +* [Single Inheritance](../single-inheritance/single-inheritance.md) +* [Multilevel Inheritance](../multilevel-inheritance/multilevel-inheritance.md) +* [super() Function in Python](../../../built-in-functions/terms/super/super.md)