Inheritance in Python is one of the foundational concepts in object-oriented programming. It allows developers to build new classes on top of existing ones, promoting code reuse, modularity, and the creation of hierarchical class structures. Python offers a simple yet powerful syntax for implementing inheritance, making it a key skill for any Python developer.
Inheritance is the mechanism by which one class (known as the child or derived class) can inherit the properties and methods of another class (known as the parent or base class). This enables us to create a new class with minimal changes to existing code.
class Parent: def greet(self): print("Hello from Parent class") class Child(Parent): def introduce(self): print("Hello from Child class") c = Child() c.greet() # Inherited from Parent c.introduce() # Defined in Child
In the above example, the Child class inherits the greet() method from the Parent class.
In single inheritance, a child class inherits from only one parent class.
class Animal: def speak(self): print("Animal speaks") class Dog(Animal): def bark(self): print("Dog barks")
In multiple inheritance, a child class inherits from more than one parent class.
class Father: def skills(self): print("Gardening") class Mother: def talents(self): print("Painting") class Child(Father, Mother): pass
Multilevel inheritance involves a class derived from a child class, which is itself derived from another parent class.
class Grandparent: def heritage(self): print("Family Heritage") class Parent(Grandparent): def culture(self): print("Family Culture") class Child(Parent): def identity(self): print("New Generation")
Multiple child classes inherit from a single parent class.
class Parent: def greet(self): print("Hello from Parent") class Child1(Parent): pass class Child2(Parent): pass
Hybrid inheritance is a combination of two or more types of inheritance.
Python uses the Method Resolution Order (MRO) to resolve ambiguity in hybrid inheritance scenarios.
In Inheritance in Python, a child class can override methods of the parent class.
class Parent: def show(self): print("Parent's method") class Child(Parent): def show(self): print("Child's overridden method") c = Child() c.show()
Here, the Child class overrides the show() method of the Parent class.
super() is a built-in function used to call methods from the parent class within a child class.
class Parent: def display(self): print("Display from Parent") class Child(Parent): def display(self): super().display() print("Display from Child") c = Child() c.display()
The super().display() call invokes the method from the parent class before executing the child’s own method.
| Type of Inheritance | Description | Example Scenario |
|---|---|---|
| Single | One child from one parent | Employee from Person |
| Multiple | One child from multiple parents | Child with talents from both parents |
| Multilevel | Chain of inheritance | Grandchild inherits grandparent traits |
| Hierarchical | Multiple children from one parent | Different employees from a common employer class |
| Hybrid | Combination of types | Complex real-world modeling |
Inheritance in Python is a powerful feature that allows you to create cleaner, more organized, and reusable code. By understanding various types of inheritance, method overriding, and using super(), developers can build scalable applications that are easy to manage and extend. Whether it's a simple script or a full-fledged application, mastering inheritance opens up new possibilities in Python development.
Copyrights © 2024 letsupdateskills All rights reserved