Method Overloading In Python



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

Please wait while submission in progress...


Contact Us

Chennai

  93450 45466

Bangalore

 93450 45466

Coimbatore

 95978 88270

Online

93450 45466

Madurai

97900 94102

Pondicherry

93635 21112

For Hiring

 93840 47472
 hr@fita.in

Corporate Training

 90036 23340


Read More Read less

FITA Academy Branches

Chennai

Bangalore

Coimbatore

Other Locations

FITA Academy - Velachery
Plot No 7, 2nd floor,
Vadivelan Nagar,
Velachery Main Road,
Velachery, Chennai - 600042
Tamil Nadu

    :   93450 45466

FITA Academy - Anna Nagar
No 14, Block No, 338, 2nd Ave,
Anna Nagar,
Chennai 600 040, Tamil Nadu
Next to Santhosh Super Market

    :   93450 45466

FITA Academy - T Nagar
05, 5th Floor, Challa Mall,
T Nagar,
Chennai 600 017, Tamil Nadu
Opposite to Pondy Bazaar Globus

    :   93450 45466

FITA Academy - Tambaram
Nehru Nagar, Kadaperi,
GST Road, West Tambaram,
Chennai 600 045, Tamil Nadu
Opposite to Saravana Jewellers Near MEPZ

    :   93450 45466

FITA Academy - Thoraipakkam
5/350, Old Mahabalipuram Road,
Okkiyam Thoraipakkam,
Chennai 600 097, Tamil Nadu
Next to Cognizant Thoraipakkam Office and Opposite to Nilgris Supermarket

    :   93450 45466

FITA Academy - Porur
17, Trunk Rd,
Porur
Chennai 600116, Tamil Nadu
Above Maharashtra Bank

    :   93450 45466

FITA Academy Marathahalli
No 7, J J Complex,
ITPB Road, Aswath Nagar,
Marathahalli Post,
Bengaluru 560037

    :   93450 45466

FITA Academy - Saravanampatty
First Floor, Promenade Tower,
171/2A, Sathy Road, Saravanampatty,
Coimbatore - 641035
Tamil Nadu

    :   95978 88270

FITA Academy - Singanallur
348/1, Kamaraj Road,
Varadharajapuram, Singanallur,
Coimbatore - 641015
Tamil Nadu

    :   95978 88270

FITA Academy - Madurai
No.2A, Sivanandha salai,
Arapalayam Cross Road,
Ponnagaram Colony,
Madurai - 625016, Tamil Nadu

    :   97900 94102

FITA Academy - Pondicherry
410, Villianur Main Rd,
Sithananda Nagar, Nellitope,
Puducherry - 605005
Near IG Square

    :   93635 21112

Read More Read less
  • Are You Located in Any of these Areas

    Adyar, Adambakkam, Anna Salai, Ambattur, Ashok Nagar, Aminjikarai, Anna Nagar, Besant Nagar, Chromepet, Choolaimedu, Guindy, Egmore, K.K. Nagar, Kodambakkam, Koyambedu, Ekkattuthangal, Kilpauk, Meenambakkam, Medavakkam, Nandanam, Nungambakkam, Madipakkam, Teynampet, Nanganallur, Navalur, Mylapore, Pallavaram, Purasaiwakkam, OMR, Porur, Pallikaranai, Poonamallee, Perambur, Saidapet, Siruseri, St.Thomas Mount, Perungudi, T.Nagar, Sholinganallur, Triplicane, Thoraipakkam, Tambaram, Vadapalani, Valasaravakkam, Villivakkam, Thiruvanmiyur, West Mambalam, Velachery and Virugambakkam.

    FITA Velachery or T Nagar or Thoraipakkam OMR or Anna Nagar or Tambaram or Porur branch is just few kilometre away from your location. If you need the best training in Chennai, driving a couple of extra kilometres is worth it!