Tutorial 8: OOPs in Python — Overloading and Overriding

Photo by Hitesh Choudhary on Unsplash

Missed previous tutorial ? : Click hare

In this tutorial we will learn about method overloading, method overriding and operator overloading.

Operator Overloading

Method Overloading

Method Overriding

Operator Overloading :

  • Overloading is achieved using Method and Operator having different functionalities with same name.
  • Operator overloading is achieved by allowing same operator to have different meanings.
  • These operators work for built-in classes. But the same operator behaves differently with different types.

Let’s see how ‘+’ operator works for built-in classes integer, string and list :

Let’s overload the ‘+’ operator for user-defined classes using special method __add__() :

Method Overloading :

  • Method Overloading can be thought as many methods with same name accepting different number of arguments within SAME class.
  • This should not be confused with Polymorphism which requires DIFFERENT classes to have same method name and different functionalities.
  • Python by default does not support Method Overloading since two methods cannot have the same name in Python.

This error can be done away with if we set the default value to the third argument as shown below :

Method Overriding :

We have already seen method overriding without explicitly mentioning it in inheritance.

One of the three purposes of of inheritance was to overwrite the method of parent class in a derived class, this is known as method overriding.

Refer Tutorial 3 for more information regarding this .

Let’s see it using example given below :

Congratulations !!

You have completed eighth tutorial. Take a deep breathe and move to the next tutorial.

--

--

--

I am a keen learner and diligent teacher with special interest in mathematics and machine learning.

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Docker Swarm continuous deployment using native service health checks

The Rebirth of Weaponized Countries

Introduction to Software testing: What is it, Why do we need it and what are the different types?

The dark side of UI test automation — Part 1

The dark side of UI test automation

Leetcode Q352. Data Stream as Disjoint Intervals (Q294)

Laravel Tip: Use CockroachDB’s Serverless database for your hobby projects

OAuth2 authentication with Spring Boot2, MySql and Angular 7

CS371p Fall 2021: Week 8

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Gaurav Patil

Gaurav Patil

I am a keen learner and diligent teacher with special interest in mathematics and machine learning.

More from Medium

Tutorial 5 : OOPs in Python — Encapsulation

Fibonacci Series in Python | 5 Best Programs

Python : Functions

Python Dictionary Methods