• Chennai, Bangalore & Online: 93450 45466Coimbatore: 95978 88270Madurai: 97900 94102

  • 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

    For Hiring

     93840 47472
     hr@fita.in

    Corporate Training

     90036 23340


    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

  • Trending Courses

    JAVA Training In Chennai Dot Net Training In Chennai Software Testing Training In Chennai Cloud Computing Training In Chennai AngularJS Training in Chennai Big Data Hadoop Training In Chennai Android Training In Chennai iOS Training In Chennai Web Designing Course In Chennai PHP Training In Chennai Digital Marketing Course In Chennai SEO Training In Chennai

    Oracle Training In Chennai Selenium Training In Chennai Data Science Course In Chennai RPA Training In Chennai DevOps Training In Chennai C / C++ Training In Chennai UNIX Training In Chennai Placement Training In Chennai German Classes In Chennai Python Training in Chennai Artificial Intelligence Course in Chennai AWS Training in Chennai Core Java Training in Chennai Javascript Training in ChennaiHibernate Training in ChennaiHTML5 Training in ChennaiPhotoshop Classes in ChennaiMobile Testing Training in ChennaiQTP Training in ChennaiLoadRunner Training in ChennaiDrupal Training in ChennaiManual Testing Training in ChennaiSpring Training in ChennaiStruts Training in ChennaiWordPress Training in ChennaiSAS Training in ChennaiClinical SAS Training in ChennaiBlue Prism Training in ChennaiMachine Learning course in ChennaiMicrosoft Azure Training in ChennaiUiPath Training in ChennaiMicrosoft Dynamics CRM Training in ChennaiUI UX Design course in ChennaiSalesforce Training in ChennaiVMware Training in ChennaiR Training in ChennaiAutomation Anywhere Training in ChennaiTally course in ChennaiReactJS Training in ChennaiCCNA course in ChennaiEthical Hacking course in ChennaiGST Training in ChennaiIELTS Coaching in ChennaiSpoken English Classes in ChennaiSpanish Classes in ChennaiJapanese Classes in ChennaiTOEFL Coaching in ChennaiFrench Classes in ChennaiInformatica Training in ChennaiInformatica MDM Training in ChennaiBig Data Analytics courses in ChennaiHadoop Admin Training in ChennaiBlockchain Training in ChennaiIonic Training in ChennaiIoT Training in ChennaiXamarin Training In ChennaiNode JS Training In ChennaiContent Writing Course in ChennaiAdvanced Excel Training In ChennaiCorporate Training in ChennaiEmbedded Training In ChennaiLinux Training In ChennaiOracle DBA Training In ChennaiPEGA Training In ChennaiPrimavera Training In ChennaiTableau Training In ChennaiSpark Training In ChennaiGraphic Design Courses in ChennaiAppium Training In ChennaiSoft Skills Training In ChennaiJMeter Training In ChennaiPower BI Training In ChennaiSocial Media Marketing Courses In ChennaiTalend Training in ChennaiHR Courses in ChennaiGoogle Cloud Training in ChennaiSQL Training In Chennai CCNP Training in Chennai PMP Training in Chennai OET Coaching Centre in Chennai

  • 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!