Overriding in Python is an important part of the object-oriented programming. The overriding mechanism is an essential part of inheritance. In order to have a better understanding of the processes the term overriding in python is to be understood.
Method overriding is a feature in object-oriented programming (OOP) where a subclass provides its own implementation of a method that is already defined in its superclass. In other words, the subclass creates a new implementation of the method that has the same name, return type, and parameters as the method in the superclass, but with a different implementation.
Here, the overriding method consists of child class's method and the overridden method consists of the parent's class method. Method overloading and method overriding are totally different concepts. When there are two functions with different parameters but with the same name it's called method overloading. It is also not directly supported in Python.
Parent class - Parent or Superclass is the inherited form of class.
Child class - Child or Subclass is the class which inherits all the methods and properties of the parent class.
Method overriding is an essential feature of object-oriented programming in Python. It allows a child class to provide its implementation for a method defined in the parent class, thereby modifying or extending the behaviour of the parent class.
Here are some key features of method overriding in Python:
Method overriding is a mechanism in object-oriented programming (OOP) that allows a subclass to provide its own implementation of a method that is already defined in its superclass. To successfully override a method, the following prerequisites must be met:
1. Inheritance - Overriding in python is a method that requires the subclass to inherit from its superclass, which means that the subclass must be a direct or indirect subclass of the superclass. So, inheritance is necessary.
2. Method Signature- It is imperative that the method in the subclass has the same name, return type, and parameters (i.e method signature) as the method in the superclass. This makes sure that the subclass method is considered an override.
3. Access Modifier - It is mandatory that the access modifier of the method in the subclass be the same as or less restrictive than that of the method in superclass. For example, if the method in the superclass is public, the method in the subclass can also be public or protected, but not private.
4. Polymorphism - Polymorphism refers to resolving method calls based on the actual type of the object that made the call, which is further described in method overriding. Also to utilise method overriding, you will need a reference of the superclass type pointing to a subclass object.
By meeting these prerequisites, you can override a method in the subclass to provide its own implementation, which will be used instead of the implementation in the superclass when the method is called on an object of the subclass.
In the given example we can observe that,
When we code the above example in python we can have a better picture of the python override method.
Now let's observe a child class - here Square will extend the shape Class.
A modified implementation of the no_of_sides() method has been added to the Square child class.
The square class now overrides the version of the parent that already exists when we call the method no_of_sides() on an object of the square class.
As well as overriding the data1 variable in the Shape parent class, the Square child class supports overriding the data1 variable-
Python programs resolve or search for classes in order of method resolution order in the case of multiple inheritances.
There are a few important points to keep in mind-
Let’s understand this with the help of an example – diamond Inheritance.
There can be ambiguity in inheritance when two classes are derived from the same superclass, but class D is derived from both class B and C. One of the main questions which arise here is which of the overridden method “demo” Class D should inherit, since it is an overridden method in both Class B and Class C.
The complexity is executed in a depth-first search fashion.
There are few methods for overriding inheritance in Python, but Diamond inheritance is a very good example.
There are a few examples which will help you understand this much better:
Scenario 1: This is an example of a multi-level inheritance where we have a base class A, and two subclasses that inherit from it: B and C, Class D inherits both B and C and also has access to the methods of A.
The code is as follows :
Here the execution order of code will be: class D -> class B -> class C -> class A .
So, the output will be as follows:
Since class B contains the demo() method, class D will execute the method of class B instead of searching for methods in other classes.
Let’s say that we passed D the parameters in another order – like class D(C,B)
The outcome in this scenario will be different : class D -> class C -> class B -> class A
In other words, it executes the method in class C itself at the first chance it gets since it finds the method in the class at the first chance.
Now, for a change let's violate the structure as shown in the tree and then pass it in the given order: class D(A,B,C):
In this case, we will definitely get an error, since D cannot start getting the method from A and move on to B then C.
This error occurs because D cannot resolve the order, going straight to A and then coming back to B and C. However, we will not experience the same problem if we pass the parameter in the following way:
The output obtained will be like this:
The order of the class parameters to the class is very important, otherwise it will result in an error if violated. Furthermore, MRO is nothing but how a class searches for any method or attribute in its parent class in terms of a DFS search.
Inheritance: There is a principle of inheritance in which an object may derive or inherit properties from another object. This promotes reusability of code. All classes inherit from each other in the same way, so every subclass of A will inherit from all subclasses of B.
The above image shows the inheritance of properties from more than one parent (base) class. In the example, D inherits properties from B and C.
Overriding in Multiple Inheritance
Upon inheriting methods/ attributes from its parent/parents, a child class can override them while inheriting them from its parent/parents.
We have already seen that the best example of overriding in multiple Inheritance is the Diamond Problem. To illustrate our concept in more detail, here is another example:
The function call here is done in a depth-first search and output is provided as a result:
Multi-level inheritance describes a relationship between a parent and child, a grandchild and a great-grandchild but with different levels of relationship. In the above image, class 3 is inherited from class 2 which, in turn, is inherited from class 1. Thus, 3 are the grandchildren, 2 are the parents, and 1 grandparent.
Overriding in Multi-level Inheritance:
Multi-level inheritance occurs when the child class(class 3) overrides the methods and attributes of the parent or grandparent class. This example represents a multi-level inheritance problem in which class 3 overrides class 2 and class 1.
In this scenario the method (home) is inherited by the Father by his Father.
The son acquires “home” from his Grandpa.
They override this function by providing different implementations for each of their ages.
As a result, we can observe that while the age function is overridden by the class Son, the home() method is inherited.
Python Override method is a fundamental concept in object-oriented programming. It allows a subclass to provide its own implementation of a method that is already defined in its superclass, enabling customization and flexibility in code design. By overriding methods, Python developers can create more specific and specialised behaviour in their subclasses, making their programs more modular, efficient, and easier to maintain. However, it's important to be mindful of how and when to use overriding, as it can also lead to unexpected behaviour and bugs if not used correctly.
If you want a hassle-free experience in your career and want to reap all the benefits of the programming language or data science collectively, then StarAgile provides Data Science certification course. Data Science is a growing field in the current era. If you complete the Data Science training provided by us, you will not only get Data Science Certification but it will also develop your skills to the next level.
>4.5 ratings in Google