For Enquiry: 93450 45466

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, Bangalore & Online

  93450 45466

Coimbatore

 95978 88270

Pondicherry

93635 21112

Madurai

97900 94102

Tirupur

99401 22502

For Hiring

 93840 47472
 hr@fita.in

Corporate Training

 90036 23340


FITA Academy Branches

Chennai

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
& Opposite to Nilgris Supermarket

    :   93450 45466

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

    :   93450 45466

FITA Academy - Pallikaranai
335A, 13th Main Rd,
Ram Nagar South Extn,
Pallikaranai, Chennai,
Tamil Nadu 600100

    :   93450 45466

Bangalore

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

    :   93450 45466

Coimbatore

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

Other Locations

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

FITA Academy - Tiruppur
61D, Poongodi Towers 2nd floor,
Periyar Colony Bus Stop,
Tirupur - 641 652

    :   9940122502

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

    Adambakkam, Adyar, Akkarai, Alandur, Alapakkam, Alwarpet, Alwarthirunagar, Ambattur, Ambattur Industrial Estate, Aminjikarai, Anakaputhur, Anna Nagar, Anna Salai, Arumbakkam, Ashok Nagar, Avadi, Ayanavaram, Besant Nagar, Bharathi Nagar, Camp Road, Cenotaph Road, Central, Chetpet, Chintadripet, Chitlapakkam, Chengalpattu, Choolaimedu, Chromepet, CIT Nagar, ECR, Eechankaranai, Egattur, Egmore, Ekkatuthangal, Gerugambakkam, Gopalapuram, Guduvanchery, Guindy, Injambakkam, Irumbuliyur, Iyyappanthangal, Jafferkhanpet, Jalladianpet, Kanathur, Kanchipuram, Kandhanchavadi, Kandigai, Karapakkam, Kasturbai Nagar, Kattankulathur, Kattupakkam, Kazhipattur, Keelkattalai, Kelambakkam, Kilpauk, KK Nagar, Kodambakkam, Kolapakkam, Kolathur, Kottivakkam, Kotturpuram, Kovalam, Kovilambakkam, Kovilanchery, Koyambedu, Kumananchavadi, Kundrathur, Little Mount, Madambakkam, Madhavaram, Madipakkam, Maduravoyal, Mahabalipuram, Mambakkam, Manapakkam, Mandaveli, Mangadu, Mannivakkam, Maraimalai Nagar, Medavakkam, Meenambakkam, Mogappair, Moolakadai, Moulivakkam, Mount Road, MRC Nagar, Mudichur, Mugalivakkam, Muttukadu, Mylapore, Nandambakkam, Nandanam, Nanganallur, Nanmangalam, Narayanapuram, Navalur, Neelankarai, Nesapakkam, Nolambur, Nungambakkam, OMR, Oragadam, Ottiyambakkam, Padappai, Padi, Padur, Palavakkam, Pallavan Salai, Pallavaram, Pallikaranai, Pammal, Parangimalai, Paruthipattu, Pazhavanthangal, Perambur, Perumbakkam, Perungudi, Polichalur, Pondy Bazaar, Ponmar, Poonamallee, Porur, Pudupakkam, Pudupet, Purasaiwakkam, Puzhuthivakkam, RA Puram, Rajakilpakkam, Ramapuram, Red Hills, Royapettah, Saidapet, Saidapet East, Saligramam, Sanatorium, Santhome, Santhosapuram, Selaiyur, Sembakkam, Semmanjeri, Shenoy Nagar, Sholinganallur, Singaperumal Koil, Siruseri, Sithalapakkam, Srinivasa Nagar, St Thomas Mount, T Nagar, Tambaram, Tambaram East, Taramani, Teynampet, Thalambur, Thirumangalam, Thirumazhisai, Thiruneermalai, Thiruvallur, Thiruvanmiyur, Thiruverkadu, Thiruvottiyur, Thoraipakkam, Thousand Light, Tidel Park, Tiruvallur, Triplicane, TTK Road, Ullagaram, Urapakkam, Uthandi, Vadapalani, Vadapalani East, Valasaravakkam, Vallalar Nagar, Valluvar Kottam, Vanagaram, Vandalur, Vasanta Nagar, Velachery, Vengaivasal, Vepery, Vettuvankeni, Vijaya Nagar, Villivakkam, Virugambakkam, West Mambalam, West Saidapet

    FITA Velachery or T Nagar or Thoraipakkam OMR or Anna Nagar or Tambaram or Porur or Pallikaranai 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!