Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How to Call a Parent class method in Python ?
In object-oriented programming, inheritance allows you to create new classes based on existing ones, providing a way to reuse code and organize your program's structure. Python, being an object-oriented language, supports inheritance and allows you to override methods defined in parent classes in child classes. However, there may be situations where you want to leverage the functionality of a parent class method while extending or modifying it in the child class.
Method Overriding in Python
Before we learn about calling parent class methods, let's briefly discuss method overriding. Method overriding is a feature in object-oriented programming that allows a subclass to provide a different implementation of a method that is already defined in its parent class. When you override a method, you redefine it in the subclass with the same name as in the parent class.
To illustrate method overriding in Python, consider the following example ?
class ParentClass:
def my_method(self):
print("This is the parent class method.")
class ChildClass(ParentClass):
def my_method(self):
print("This is the child class method.")
# Creating instances and calling methods
parent = ParentClass()
child = ChildClass()
parent.my_method()
child.my_method()
This is the parent class method. This is the child class method.
As you can see, calling my_method() on an instance of ParentClass invokes the parent class implementation, while calling it on an instance of ChildClass invokes the overridden method in the child class.
Using super() to Call Parent Class Methods
In Python, you can call the parent class method from within the overridden method using the super() function. The super() function returns a temporary object of the parent class, allowing you to access its methods.
The general syntax for calling a parent class method using super() is as follows ?
class ChildClass(ParentClass):
def overridden_method(self):
super().method_name(arguments)
# Additional code in the child class method
Here's a practical example demonstrating how to call the parent class method using super() ?
class ParentClass:
def my_method(self):
print("This is the parent class method.")
class ChildClass(ParentClass):
def my_method(self):
super().my_method() # Calling the parent class method
print("This is the child class method.")
# Creating instance and calling method
child = ChildClass()
child.my_method()
This is the parent class method. This is the child class method.
In this example, the ChildClass overrides the my_method() defined in the ParentClass. Inside the overridden method, we use super().my_method() to call the parent class method first, followed by the additional code in the child class.
Practical Examples
Vehicle Inheritance Example
class Vehicle:
def start(self):
print("Vehicle started.")
class Car(Vehicle):
def start(self):
super().start() # Call the parent class method
print("Car engine running.")
car = Car()
car.start()
Vehicle started. Car engine running.
Constructor Chaining Example
class Shape:
def __init__(self, color):
self.color = color
def draw(self):
print(f"Drawing a shape with color: {self.color}")
class Circle(Shape):
def __init__(self, color, radius):
super().__init__(color) # Call parent constructor
self.radius = radius
def draw(self):
super().draw() # Call the parent class method
print(f"Drawing a circle with radius: {self.radius}")
circle = Circle("Red", 5)
circle.draw()
Drawing a shape with color: Red Drawing a circle with radius: 5
Benefits of Using super()
| Benefit | Description | Advantage |
|---|---|---|
| Code Reusability | Reuse parent class functionality | Reduces code duplication |
| Maintainability | Changes in parent propagate automatically | Easier to maintain codebase |
| Extensibility | Add functionality to existing methods | Flexible customization |
Conclusion
Use super() to call parent class methods in Python, enabling code reuse and proper inheritance. This technique allows you to extend parent functionality while maintaining the original behavior, promoting cleaner and more maintainable object-oriented code.
