Introduction
Welcome to my 72nd blog post on the Python journey. On day 72, I learned about 'super' keyword that is used to refer to the parent class. It is used when a class inherits from multiple parent classes and we want to call a method from one of the parent classes. Let's dive into more details and understand the use of the 'super' keyword along with the examples.
So let's get started......
'super' Keyword in Python
When we inherit a class from the parent class, it extends or overwrites the methods of the parent class.
However, sometimes we want to use the methods of the parent class in child class.
Then we make use of 'super' Keyword in Python
Example -
class ParentClass:
def parent_method(self):
print("This is the parent method.")
class ChildClass(ParentClass):
def child_method(self):
print("This is the child method.")
super().parent_method() #use super keyword to access parent method
child_object = ChildClass()
child_object.child_method()
Output
This is the child method.
This is the parent method.
In the above example, we have ParentClass and ChildClass that inherits the ParentClass and overrides the child_method. When the child_method is called, it first prints "This is the child method." from ChildClass and then calls the parent_method using the super() keyword.
Using 'super' with multiple parents
We can use the super keyword, when a class inherits from multiple parent classes.
In this case, you can specify the parent class from which you want to call the method.
class ParentClass1:
def parent_method(self):
print("This is the parent method of ParentClass1.")
class ParentClass2:
def parent_method(self):
print("This is the parent method of ParentClass2.")
class ChildClass(ParentClass1, ParentClass2): #can interchange the order
def child_method(self):
print("This is the child method.")
super().parent_method()
child_object = ChildClass()
child_object.child_method()
In this example, the ChildClass inherits from both ParentClass1 and ParentClass2. The child_method calls the parent_method of the first parent class using the super() keyword.
Note: In the above example one can interchange the order for the 'super' keyword to gain the importance of the parent class.
More Example
Here we used super to gain access to the constructor in the Employee class.
class Employee:
def __init__(self, name, id):
self.name = name
self.id = id
class Programmer(Employee):
def __init__(self, name, id, lang):
super().__init__( name, id)
self.lang = lang
rohan = Employee("Rohan Das", "420")
harry = Programmer("HarryPuttar", "2345", "Python")
print(harry.name)
print(harry.id)
print(harry.lang)
Output
HarryPuttar
2345
Python
Resources Used
You can watch the video of Day#72 by clicking on the below link ๐๐๐๐๐
Conclusion
Thanks, guys for going through this blog post. On day 72, I learned about the 'super' Keyword and how it can be used to call a parent class method in a child class. It can also be used in case of multiple inheritance scenarios when a child class inherits multiple parent classes.
Thank you if you read this post and have found this post useful. I hope you have joined me and are enjoying my magical journey of Python coding. This is it for Day #72
See you in the next one.....
About Me
Hey Guys, I am Chintan Jain from CodeWithJain. I am a trader and content creator. I am also passionate about tech and hence wanted to explore the field of tech. I always wanted to learn to code so I watched many tutorials but procrastinated practicing coding. To get into the habit of coding consistently I am starting to BLOG with HASHNODE on daily basis.
I will document my coding journey from scratch and share my daily learnings in a blog post on HASHNODE. I hope you all will enjoy my content and my coding journey.
So what are you waiting for, smash the FOLLOW and LIKE buttons and follow along my coding journey, a step to create more beautiful digital products and empower people.