Python From Beginner to Advanced

0% completed

Previous
Next
Python - Inheritance

Inheritance is a pivotal feature of object-oriented programming that facilitates the extension of functionality by deriving new classes from existing ones. It simplifies the code, enhances reusability, and provides a natural way to organize related classes.

Why Use Inheritance?

  1. Code Reuse: By inheriting from a base class, a derived class can reuse code without redundancy, leading to a more maintainable and error-free application.
  2. Extensibility: Changes to the base class can improve or modify derived classes without altering their definitions, facilitating easy updates and enhancements.
  3. Simplicity: Structuring relationships between classes through inheritance mirrors real-world hierarchies, simplifying complex interactions and dependencies.

Types of Inheritance in Python

Python supports various forms of inheritance, which enable flexible class designs:

  1. Single Inheritance: Involves a child and a parent class.
  2. Multiple Inheritance: A class can inherit from more than one base class.
  3. Multilevel Inheritance: A class inherits from a derived class, making it a derived class through multiple levels.
  4. Hierarchical Inheritance: Multiple classes inherit from a single parent class.

Single Class Inheritance

Single inheritance enables a derived class to inherit properties from a single parent class. This is the most straightforward type of inheritance, which helps in extending the functionality of the base class.

In Python, inheritance is implemented by passing the parent class as a parameter to the declaration of the child class.

Image

Example

Python3
Python3

. . . .

Explanation:

  • class Animal: - Defines the base class with a method to initialize its name and a generic speak method.
  • class Dog(Animal): - The Dog class inherits from Animal, indicating that Dog is a type of Animal.
  • return "Woof!" - Overrides the inherited speak method to provide a specific behavior for dogs.
  • Dog("Buddy") - An instance of Dog is created, which calls the Animal constructor to set the name.
  • my_dog.speak() - Calls the overridden method, which now behaves differently from the base class.

Multiple Inheritance

Multiple inheritance allows a class to inherit from more than one base class. Python supports this feature, enabling the creation of a class that can exhibit behaviors inherited from multiple parent classes.

Example

Python3
Python3

. . . .

Explanation:

  • class Mother: and class Father: - Define two base classes with distinct methods.
  • class Child(Mother, Father): - Inherits from both Mother and Father, combining their methods into one class.
  • self.intelligence() and self.strength() - Methods from both Mother and Father are called within the Child class method, demonstrating how attributes from both parents are accessible in Child.

Multilevel Inheritance

Multilevel inheritance involves a hierarchy of classes that extends across more than two levels. This allows for a base class to be extended by a child class, which in turn can be extended by another child class.

Image

Example

Python3
Python3

. . . .

Explanation:

  • class Grandparent: - The top-level base class with a method defining its origin.
  • class Parent(Grandparent): - Inherits from Grandparent, adding another layer of functionality.
  • class Child(Parent): - The lowest level in the hierarchy, Child inherits properties and methods from both Parent and Grandparent.
  • self.origin() and self.heritage() - Child accesses methods from its parent and grandparent, showing multilevel inheritance capabilities.

Hierarchical Inheritance

Hierarchical inheritance involves multiple classes deriving from a single base class. This type of inheritance allows various derived classes to share a common set of features and behaviors defined in a single base class, each potentially extending that base with specific functionalities.

Image

Example

Python3
Python3

. . . .

Explanation:

  • class Animal: - The base class Animal defines common attributes and methods such as eat, which all animals can use.
    • self.kingdom = 'Animalia' - Sets a common characteristic for all animals.
    • def eat(self): - Defines a common behavior that all animal types can inherit and use.
  • class Dog(Animal): and class Cat(Animal): - Both Dog and Cat inherit from Animal.
    • def speak(self): - Each subclass overrides the speak method to provide specific behavior for dogs and cats.
  • dog = Dog() and cat = Cat() - Instances of Dog and Cat are created.
    • dog.eat() and cat.eat() - Both Dog and Cat instances use the eat method inherited from Animal.
    • dog.speak() and cat.speak() - Demonstrate specific behaviors defined in their respective classes.

This structured explanation covers various inheritance types in Python, demonstrating how they can be used to build complex class hierarchies and enhance the functionality of Python applications. Each type of inheritance serves different use cases and can be selected based on the needs of the application design.

.....

.....

.....

Like the course? Get enrolled and start learning!
Previous
Next