Python Tutorial
About Lesson

In this lesson we want to learn about Python Method Overriding, Python Method Overriding is feature of Object Oriented Programming that allows a subclass to provide its own implementation of method that is already defined in its superclass. when method is called on an object of the subclass the overridden method in the subclass is executed instead of the method in the superclass. 

Method Overriding is useful when you want to change the behavior of a method in subclass without modifying the original implementation in the superclass. this allows you to customize the behavior of the method to better suit the needs of the subclass, while still retaining the original functionality of the method in the superclass.

In Python, Method Overriding is achieved by defining method in the subclass with the same name as the method in the superclass. when the method is called on an object of the subclass, the interpreter will first look for the method in the subclass. if it finds the method in the subclass, it will execute that method. If it does not find the method in the subclass, it will look for the method in the superclass.

 

 

Now let’s create a practical example.

So in the above example we have two classes, the first class is our Person Class and it is our base class, also we have added a method of def print_name() in this class, on the other hand we have another class at name of Student class and this class is extending from the base class, so for this we can say that it is a derived class, now you can see that we have the same name method in our derived class, when you have two methods with the same name and different implementation that is called Method Overriding.

 

 

 

If you run the code you can see that we have different implementation for the same method name.

Python Method Overriding
Python Method Overriding