Method Overriding in Python: What is it, How to do it?

Python is a high-level, general-purpose language that is a must-learn for computer programming enthusiasts. Similar to other widely-used computer languages like JAVA, C++, Golang, Ruby, etc. Python is also an object-oriented programming language where coding is dependent on classes and objects. The concept allows developers to structure simple, reusable codes which then denote individual objects. There are several useful features that OOP languages offer, one of which is Method Overriding.

In this article, we will understand the concept of inheritance to explore method overriding in Python. Let’s begin with understanding what a class is.

What is a Class?

Class is a feature or instance in the Object-Oriented programming paradigm. It is an extended program coding template within a program. Classes help programmers input the initial data of each object (arrays, strings, variables, etc.) at the time of creation. They also enable us to define the characteristics of the class and determine how they will process data or provide input. There are sub-classes or child-classes under the defined parent-classes or superclasses in a program.

What is Inheritance? What are its benefits?

Inheritance in object-oriented programming is a property that allows a class’s features to be inherited to other classes or subclasses. This helps prevent data redundancy, compile the program better, and limit the scope of making mistakes. 

This capability of a class to derive or inherit features from other classes is known as inheritance. The class whose characteristics and properties are used to derive a subclass is called the Parent class.

Here are the benefits of inheritance: 

  • Inheritance allows users to represent real-time relationships well. 
  • It allows codes to be reused. It also allows programmers to provide more characteristics to a class.
  • It allows for transition. Suppose the class Q derives the features from class W, then the sub-classes of Q would automatically inherit from class W.

Types of Inheritance

There are three types of inheritances which are as follows;

1. Single Inheritance

In single inheritance, a derived class can inherit features from a single parent class. It enables codes to be reused and also the adding of new features.

2. Multiple Inheritance

It refers to transferring the characteristics of more than one parent class to a subclass. Here, the features of the base class are inherited into the sub-class automatically.

3. Multilevel inheritance

When the features of the parent class and the sub-class are further inherited into another derived class, it is called multilevel inheritance.

What is Method Overriding in Python?

Method overriding is a feature of object-oriented programming languages where the subclass or child class can provide the program with specific characteristics or a specific implementation process of data provided that are already defined in the parent class or superclass.

When the same returns, parameters, or name is input in the subclass as in the parent class, then the method of implementation in the subclass overrides the method as mentioned in the parent class. This is known as method overriding.

Its execution depends on the data that is used to invoke the method and not the reference data already provided in the parent class. If an object of the parent class is used to invoke the method of implementation that is specific to a program, then the version of the method as written in the parent class is invoked. On the other hand, if an object of the subclass is used to invoke the method, the execution will be according to the features mentioned in the subclass. If you are a beginner and would like to gain expertise in data science, check out our data science courses.

Features of Method Overriding

This section is going to explore the salient features that method overriding in python offers:

  • Method overriding allows the usage of functions and methods in Python that have the same name or signature.
  • Method overloading is an example of runtime polymorphism.
  • In method overriding, using the feature of inheritance is always required.
  • Method overloading is carried out between parent classes and child classes.
  • It is used to change the behaviour and implementation of existing methods.
  • There is always a need for a minimum of two classes for method overriding.

Understanding Method Overriding: Multiple and Multilevel Inheritance

Multiple and multilevel inheritances are two types of inheritances offered by most object-oriented programming languages. In this section, we will see how method overriding is used in various and multilevel inheritances:

1. Multiple Inheritance

In this kind of inheritance of class or object, a subclass inherits features and characteristics of multiple parent classes or bases. For example, there are three different parent classes- class.1, class.2, class.3, and there is class.a, which is the child class executing features of multiple inheritances. The class.a will contain features inherited from all three base classes.

Here is a sample program:

# Defining parent class 1 

class Parent1(): 

    # Parent’s show method 

    def show(self): 

        print(“Inside Parent1”) 

# Defining Parent class 2 

class Parent2(): 

    # Parent’s show method 

    def display(self): 

        print(“Inside Parent2”) 

# Defining child class 

class Child(Parent1, Parent2): 

    # Child’s show method 

    def show(self): 

        print(“Inside Child”)   

# Driver’s code 

obj = Child() 

obj.show() 

obj.display()

Output:

Inside Child

Inside Parent2

2. Multilevel Inheritance

In this kind of inheritance of class or object, a subclass is directly inherited from the base class, already bearing characteristics of the parent class. It is basically like the father-son or grandfather-grandchild relationship. The features of the subclass are unique to its parent class. For example, there is a parent class named class.Q, and a subclass is formed in the extension of the class.Q. Here, the sub-class can act as the base class for another sub-class.

Here is a sample program:

class Parent():  

    def display(self): 

        print(“Inside Parent”) 

class Child(Parent): 

    # Child’s show method 

    def show(self): 

        print(“Inside Child”) 

class GrandChild(Child):  

    # Child’s show method 

    def show(self): 

        print(“Inside GrandChild”)          

# Driver code  

g = GrandChild()    

g.show() 

g.display()

Output:

Inside GrandChild

Inside Parent

Invocation of Base Class Methods within Overridden Methods

There is a method where parent class methods can be invoked in between overridden methods. It can be done by using the following processes:

  •  Using class name

writing class name method in the overridden method in the subclass can invoke the method provided in the parent class.

  • Using the function of super()

the programming language of Python offers a function called super(), which is used to refer to the parent class especially.

Conclusion

Method overriding is one of the most crucial features of the Python language. The property finds extensive use in programming data science applications and allows for compact and efficient data analysis. Method overriding also makes the compilation of codes and rechecking easier. There are some prerequisites to using this feature: understanding the basic concept of classes and inheritance. 

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. 

Reach out to us today to start your learning journey! 

Differentiate between overriding and overloading.

The primary difference between overriding and overloading is that overriding takes place between the same methods or the same signatures, whereas overloading occurs between the same class methods.

1. Method overloading exemplifies compile-time polymorphism, whereas method overriding exemplifies runtime polymorphism.
2. Method overloading cannot be executed in Python, whereas executing method overriding is possible under certain conditions.

What are the features of Method overriding?

Here are the features of method overriding:

1. It exemplifies runtime polymorphism.
2. It establishes a link between the parent class and the child class.
3. Since it connects at least one class, We need to use the feature of inheritance.
4. Method overriding helps you to use a single name for more than one function.

We use method overriding to implement the method of the parent class in the derived class.

What are the different types of inheritances? Is it possible to override private methods in Python?

Primarily, method overriding uses multilevel and multiple inheritances.

In Multilevel inheritance, the subclass is inherited from a base class consisting of characteristics of the parent class. But in the case of multiple inheritance, a subclass shows characteristics of more than one or more parent classes or bases.

Private method overriding cannot happen in Java, but it is doable in Python using a process known as ‘name mangling’.

Master the Data Science with Certification from IIITB

0 replies on “Method Overriding in Python: What is it, How to do it?”

×