Python – Method Overloading

Click the link: “Python – Method Overloading

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.

Read more…

Advertisements

Python – Method Overriding

Click the link: “Python – Method Overriding

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 SuperAwesome.

# parent class
class Awesome:

    def greetings(self, message = "Hello World"):
        print("Greetings from Awesome:", message)

# child class
class SuperAwesome(Awesome):

    # overriding the method of the parent class
    def greetings(self, message = None):
        if message != None:
            print("Greetings from SuperAwesome:", message)
        else:
            print("Greetings from SuperAwesome!")

# objects
pObj = Awesome()        # parent class object
cObj = SuperAwesome()   # child class object

# method call
pObj.greetings()
pObj.greetings('Hello!!!')

cObj.greetings()
cObj.greetings('Hello!!!')

Read more…