Let’s understand Method Overloading In Python deeply with this table of content…

What is Overloading in Python
Operator Overloading With Examples
How To Overload Method In Python 
Example Programs For Method Overloading
Method Overriding
Example Programs For Method Overriding

Let us now learn the Method Of Overloading in Python, starting with what is Overloading In Python.

What Is Overloading In Python?

Overloading is giving different definitions to a method or function in different places i.e., the method will work differently but with the same name.

Overloading the method, prioritizes the DRY(Don’t Repeat Yourself) rule, by code redundancy, reusability. Overloading avoids complexities in code and improves code clarity.

After understanding what is Overloading in python, let us now see what is operator overloading in python along with example programs.

Operator Overloading With Examples

In python, the operator behaves differently based on the type of object, this also a type of polymorphism, but as operator overloading.

For example

+ (operator) sums up two numbers or concatenates the two or more in case of strings.

>> 3+3

6

>> ‘3’+’3′

33

* multiplies two numbers or repeats the object in case of use between strings.

>> 3*3

9

>> ‘3’*3

333

After understanding what is Operator Overloading in python, let us now see how to overload a method in python along with an example program.

How to Overload Method In Python

In python, you can overload the same method or function defined in a place, with a different number of arguments or zero arguments while using the same name at different places. This process is known as Method Overloading.

Example Program For Method Overloading

class Car:

     def properties(self, name=None):

         if name is not None:

               print(‘This is the brand new’ + name)

         else: 

               print(‘The car has not been named yet!’)

# Creating instance

obj = Car()

# Calling the method without parameter

obj.properties()

 

The above program outputs

The car has not been named yet!

Now if we try to pass an argument to the object for the same function 

# Calling the method with a parameter

obj.properties(‘FITA’)

And it outputs…

This is FITA

Check out this complete Online Python Course by FITA. FITA provides a complete Python course which covers all the basics and advanced concepts of python including exception handlings, regular expressions, along with building real-time projects like Bitly and Twitter with Django, bundled with placement support, and certification at an affordable price with an active placement cell, to make you industry required certified developer.

After understanding how to Overloading a method in python, let us now see what is method overriding method in python along with an example program.

Method Overriding

Method overriding is when you are using the same method name, and the same number of arguments in the classes and usually in case of inheritance.

Let’s say you want to give different discounts to your regular and occasional customers because regular customers know your previous prices well and are good at a bargain, although you can convince occasional customers with a less discount.

Here’s a simple code that will have the same functionalities but different discount rate,

# Example program for Method Overriding


class RegularCustomer:


       def __init__(self, name, item, price, quantity):

           self.name = name

           self.item = item

           self.price = price

           self.quantity = quantity


     def discount(self):

          return self.price * 0.3


class OccasionalCustomer(RegularCustomer):

    def __init__(self, name, item, price, quantity):

       super().__init__(name, item, price, quantity)


   def discount(self):

       return self.price * 0.1


# common interface

def cal_discount(object):

    z = object.discount()

    print(f'{object.name}\’s discount will be {z}’)

    print(f'{object.final()} is your final price’)

The above program has two different classes RegularCustomer and OccasionalCustomer.The __init__ method also known as the dunder or magic method is a constructor for the class and initializes the attributes to be used within the class.

The super() charge method and the subclasses initializes all the attributes passed and all the methods of the base class into the child class.

A discount method defined above for the regular customers calculate a 30 percent discount, whereas it has been defined again in the child class to return a 10 percent discount. This is method overriding, if we wouldn’t have defined the discount method again, it will calculate a 30 percent discount for both the types of customers.

Let’s test the program now by creating objects of this class and calling methods,

cus_1 = RegularCustomer(‘Atufa’, ‘shoes’, 600, 1)

cus_2 = OccasionalCustomer(‘Shireen’, ‘shoes’, 600, 1)


# passing the objects to common interface

print(cal_discount(cus_1))

print(cal_discount(cus_2))

The above program will now output

Atufa’s discount will be 180.0

Shireen’s discount will be 60.0

Let’s now add one more method which will give us the final price of the program.

Add this method only in the base class and you will be good to go..

def final(self):

return (self.price – self.discount()) * self.quantity

This will now output as follows

Atufa’s discount will be 180.0

420.0 is Atufa final price


Shireen’s discount will be 60.0

540.0 is Shireen final price

You can now leave the first print function to see the discount and leave it to your program.

You can find this complete code at my Github account over here.

This was all about what is method overloading and method overriding and how to implement it along with example programs. To get in depth knowledge of Python along with its various applications and real-time projects, you can enroll in Python Training in Chennai or Python Course in Bangalore by FITA which covers all the basics and advanced concepts of python including exception handlings, regular expressions, along with building real-time projects like Bitly and Twitter with Django, at an affordable price, which includes certification, support, and career guidance assistance with an active placement cell, to make you industry required certified python developer.

FITA’s courses training is delivered by professional experts who have worked in the software development and testing industry for a minimum of 10+ years, and have experience of working with different software frameworks and software testing designs.






Quick Enquiry