In this tutorial we will learn about method overloading in Python.
We learned about method overriding in the Python – Method Overriding tutorial. Feel free to check that out.
What is method overloading?
In Python we can create a method that can be called in different ways.
So, we can have a method that has zero, one or more number of parameters and depending on the method definition we can call it with zero, one or more arguments.
This is method overloading in Python.
In this tutorial we will learn about method overriding in Python.
What is method overriding?
When a method in a child class has the same name and type signature as a method in the parent class then the child class method is said to override the parent class method and this is method overriding.
Example of method overriding
In the following Python program we are overriding the
greetings method of the parent class
Awesome in the child class
# parent class
def greetings(self, message = "Hello World"):
print("Greetings from Awesome:", message)
# child class
# overriding the method of the parent class
def greetings(self, message = None):
if message != None:
print("Greetings from SuperAwesome:", message)
print("Greetings from SuperAwesome!")
pObj = Awesome() # parent class object
cObj = SuperAwesome() # child class object
# method call