Blog_Banner_Asset
    Homebreadcumb forward arrow iconBlogbreadcumb forward arrow iconData Sciencebreadcumb forward arrow iconMethod Overriding in Python

Method Overriding in Python

Last updated:
27th Jan, 2023
Views
Read Time
3 Mins
share image icon
In this article
Chevron in toc
View All
Method Overriding in Python

Prerequisites to understand Method Overriding:

Method overriding is one of the most significant concepts in object-oriented programming. The implementation of a specific function in the child class defined in a parent class can be changed by the Method Overriding. Method overriding can be defined as the capability of the child class to alter the execution of a method that is offered by one of its parent class, referred to as ancestors. To override a function, the following criteria are to be met by the class.

  • Inheritable class: Performing function overriding within a class is not possible. Derivation of a child class from a parent class is necessary.
  • The signature of the function redefined in the child class and that in the parent class should be the same. In other words, the overridden function and the function in the parent class should have the same number of arguments and parameters.

An Overview of Method Overriding in Python:

As defined earlier, the method overriding property of an object-oriented programming language by virtue of which the subclass provides a particular implementation of the method that is already offered by one of its super-classes. The method in a subclass (child class) is said to override a super class (parent class) method when the subclass method has the same parameters or signature, same name and identical return type or sub-type as the method in its super class. 

The object used to invoke an executed method determines its version. The version in the superclass will be executed if an object in the superclass is used to call the method. However, if a method is invoked by an object in the subclass, then the child class’s version will be executed.  In other terms, the type of the object that is referred to is used to determine the version of the method that is overridden and executed. The overridden method’s version is not determined by the type of the reference variable.

Check Out upGrad’s Data Science Courses 

Benefits of Method Overriding:

The primary advantage of method overriding in Python is that the class offers a model defined by itself to the inherited method without altering the code of the parent class. This feature is also beneficial in cases where a child class uses a parent class with multiple child classes. In this case, the method and parameters of the parent class can be invoked by the child class without modifying the code of the parent class.

Explore our Popular Data Science Courses

Read our popular Data Science Articles

Differences between Method Overriding and Method Overloading

Method Overriding

Method Overloading

Methods or functions in both parent and child classes must have the same signatures and same names.The functions or methods must have the same names. However, their signatures need not be the same.
This is an example of runtime polymorphism.This process is an example of compile-time polymorphism.
Inheritance is a must for method overriding in Python.Inheritance of classes may be required in some cases. However, it may not be required in all cases.
Method overriding happens between the methods in the parent class and child class. However, it cannot happen within a class.Method overloading happens between the methods within the same class.
This feature is used to alter the behavior of the existing methods.It is used to add more to the method’s behavior.
At least two classes (a child class and a parent class) are required to perform method overriding.No additional classes are required because overloading happens among the methods within the same class.

If you’d like to learn in-depth about method overriding in Python, we suggest you take up the 18-month Master of Science in Data Science. upGrad offers this course from IIIT Bangalore and Liverpool John Moore’s University. The course teaches you skills like Statistics, Predictive Analytics using Python, Machine Learning, Data Visualization, Big Data Analytics, and more. Students with a strong knowledge of concepts of Python are well-positioned to tackle subjects like Deep Learning, Natural Language Processing, Business Analytics, and Data Engineering. 

Also, Check out all trending Python tutorial concepts in 2024.

Profile

Rohit Sharma

Blog Author
Rohit Sharma is the Program Director for the UpGrad-IIIT Bangalore, PG Diploma Data Analytics Program.

Explore Free Courses

Suggested Blogs

Top 13 Highest Paying Data Science Jobs in India [A Complete Report]
905286
In this article, you will learn about Top 13 Highest Paying Data Science Jobs in India. Take a glimpse below. Data Analyst Data Scientist Machine
Read More

by Rohit Sharma

12 Apr 2024

Most Common PySpark Interview Questions & Answers [For Freshers & Experienced]
20936
Attending a PySpark interview and wondering what are all the questions and discussions you will go through? Before attending a PySpark interview, it’s
Read More

by Rohit Sharma

05 Mar 2024

Data Science for Beginners: A Comprehensive Guide
5069
Data science is an important part of many industries today. Having worked as a data scientist for several years, I have witnessed the massive amounts
Read More

by Harish K

28 Feb 2024

6 Best Data Science Institutes in 2024 (Detailed Guide)
5181
Data science training is one of the most hyped skills in today’s world. Based on my experience as a data scientist, it’s evident that we are in
Read More

by Harish K

28 Feb 2024

Data Science Course Fees: The Roadmap to Your Analytics Career
5075
A data science course syllabus covers several basic and advanced concepts of statistics, data analytics, machine learning, and programming languages.
Read More

by Harish K

28 Feb 2024

Inheritance in Python | Python Inheritance [With Example]
17652
Python is one of the most popular programming languages. Despite a transition full of ups and downs from the Python 2 version to Python 3, the Object-
Read More

by Rohan Vats

27 Feb 2024

Data Mining Architecture: Components, Types & Techniques
10806
Introduction Data mining is the process in which information that was previously unknown, which could be potentially very useful, is extracted from a
Read More

by Rohit Sharma

27 Feb 2024

6 Phases of Data Analytics Lifecycle Every Data Analyst Should Know About
80800
What is a Data Analytics Lifecycle? Data is crucial in today’s digital world. As it gets created, consumed, tested, processed, and reused, data goes
Read More

by Rohit Sharma

19 Feb 2024

Sorting in Data Structure: Categories & Types [With Examples]
139152
The arrangement of data in a preferred order is called sorting in the data structure. By sorting data, it is easier to search through it quickly and e
Read More

by Rohit Sharma

19 Feb 2024

Schedule 1:1 free counsellingTalk to Career Expert
icon
footer sticky close icon