Inheritance in Python
Understanding Inheritance in Python: A Comprehensive Guide
Inheritance is a fundamental concept in object-oriented programming (OOP) that enables the creation of a new class by utilizing the properties and behaviors of an existing class. In Python, a versatile and dynamically typed language, inheritance plays a crucial role in code organization, reusability, and extensibility. Let’s explore the intricacies of inheritance in Python.
- Basic Concepts: In Python, a class can inherit attributes and methods from another class, referred to as the base or parent class. The new class is called the derived or child class. To establish this relationship, the child class is defined with the name of the parent class in parentheses after its own name.
class Parent:
def __init__(self, name):
self.name = name
def display_name(self):
print(f"Parent's name: {self.name}")
class Child(Parent):
pass2. Single Inheritance: Python supports single inheritance, meaning a class can inherit from only one parent class. In the example above, the Child class inherits from the parent class. The child class can access the attributes and methods of the parent class.
3. Overriding Methods: Child classes have the flexibility to override methods inherited from the parent class. This allows for customization of behavior while maintaining the overall structure.
class Child(Parent):
def display_name(self):
print(f"Child's name: {self.name}")4. Multiple Inheritance: Python also supports multiple inheritance, allowing a class to inherit from more than one parent class. This can be achieved by listing multiple parent classes in the parentheses during class definition.
class Parent1:
pass
class Parent2:
pass
class Child(Parent1, Parent2):
pass5. Method Resolution Order (MRO): In cases of multiple inheritance, Python follows the C3 linearization algorithm to determine the method resolution order. This ensures a consistent and predictable order in which base classes are considered when resolving method calls.
Get Vipul Kaitke’s stories in your inbox
Join Medium for free to get updates from this writer.
6. Super Function: The super() function is commonly used in Python to call a method from a parent class. It allows for better code maintenance and avoids hardcoding class names.
class Child(Parent):
def display_name(self):
super().display_name()
print("Additional functionality in Child class.")7. Abstract Classes: Python supports abstract classes using the ABC (Abstract Base Class) module. Abstract classes cannot be instantiated and must be subclassed. Abstract methods defined in the parent class must be implemented by the child class.
from abc import ABC, abstractmethod
class Shape(ABC):
@abstractmethod
def area(self):
pass
class Circle(Shape):
def __init__(self, radius):
self.radius = radius
def area(self):
return 3.14 * self.radius * self.radiusIn conclusion, inheritance in Python is a powerful mechanism that promotes code reuse, extensibility, and organization. Whether employing single or multiple inheritance, understanding the method resolution order and utilizing the super function contributes to creating robust and maintainable code in Python’s object-oriented paradigm.
#Happy Coding






