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.

Want to share this article?

Prepare for a Career of the Future

Leave a comment

Your email address will not be published. Required fields are marked *

Our Popular Data Science Course

Get Free Consultation

Leave a comment

Your email address will not be published. Required fields are marked *

Get Free career counselling from upGrad experts!
Book a session with an industry professional today!
No Thanks
Let's do it
Get Free career counselling from upGrad experts!
Book a Session with an industry professional today!
Let's do it
No Thanks