0% completed
Python - Inheritance
On This Page
Why Use Inheritance?
Types of Inheritance in Python
Single Class Inheritance
Example
Multiple Inheritance
Example
Multilevel Inheritance
Example
Hierarchical Inheritance
Example
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?
- 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.
- Extensibility: Changes to the base class can improve or modify derived classes without altering their definitions, facilitating easy updates and enhancements.
- 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:
- Single Inheritance: Involves a child and a parent class.
- Multiple Inheritance: A class can inherit from more than one base class.
- Multilevel Inheritance: A class inherits from a derived class, making it a derived class through multiple levels.
- 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.
Example
Explanation:
class Animal:- Defines the base class with a method to initialize its name and a genericspeakmethod.class Dog(Animal):- TheDogclass inherits fromAnimal, indicating thatDogis a type ofAnimal.return "Woof!"- Overrides the inheritedspeakmethod to provide a specific behavior for dogs.Dog("Buddy")- An instance ofDogis created, which calls theAnimalconstructor 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
Explanation:
class Mother:andclass Father:- Define two base classes with distinct methods.class Child(Mother, Father):- Inherits from bothMotherandFather, combining their methods into one class.self.intelligence()andself.strength()- Methods from bothMotherandFatherare called within theChildclass method, demonstrating how attributes from both parents are accessible inChild.
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.
Example
Explanation:
class Grandparent:- The top-level base class with a method defining its origin.class Parent(Grandparent):- Inherits fromGrandparent, adding another layer of functionality.class Child(Parent):- The lowest level in the hierarchy,Childinherits properties and methods from bothParentandGrandparent.self.origin()andself.heritage()-Childaccesses 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.
Example
Explanation:
class Animal:- The base classAnimaldefines common attributes and methods such aseat, 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):andclass Cat(Animal):- BothDogandCatinherit fromAnimal.def speak(self):- Each subclass defines its ownspeakmethod to provide specific behavior for dogs and cats.
dog = Dog()andcat = Cat()- Instances ofDogandCatare created.dog.eat()andcat.eat()- BothDogandCatinstances use theeatmethod inherited fromAnimal.dog.speak()andcat.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.
Reading Progress
0%
On This Page
Why Use Inheritance?
Types of Inheritance in Python
Single Class Inheritance
Example
Multiple Inheritance
Example
Multilevel Inheritance
Example
Hierarchical Inheritance
Example