Python Method Overriding

In this article we are going to learn about Python Method Overriding, method overriding is used for changing the implementation of a method provided by one of it is parent class. so in method overriding you can use the same name methods in your base class and your derived class, or we can say in Python, method overriding is process in which child class provides its implementation of method that is already defined in its parent class. When child class defines method with the same name as method in the parent class, it is said to be overriding the parent class method. when method is called on an instance of the child class, Python first looks for the method definition in the child class. If the method is not found, it looks for it in the parent class. If the method is found in the parent class, but also overridden in the child class, Python uses the child class implementation.

 

 

 

Benefits of Python Method Overriding

Benefits of method overriding in Python are:

  1. Flexibility: Method overriding allows child classes to change the behavior of method inherited from parent class according to their specific needs. this provides flexibility in the design and implementation of classes and can save a lot of time and effort.
  2. Code reuse: Method overriding allows child classes to reuse code written in parent class without having to write it again. this helps in reducing code duplication and improving code maintainability.
  3. Polymorphism: Method overriding is an important feature of object oriented programming that enables polymorphism. Polymorphism allows objects of different classes to be treated as if they are of the same type, as long as they have the same method name and signature.
  4. Encapsulation: Method overriding provides a way to encapsulate behavior in classes. the parent class can define a method as a template for child classes, and child classes can override this method to provide their own behavior while still adhering to the same interface.

In result we can say that method overriding is powerful feature that can make code more flexible, maintainable and reusable. It allows developers to create complex, hierarchical class structures that can adapt to changing requirements and use cases.

 

 

 

Now let’s take a look at this example.

In the above example we have two classes, the first class is our Person Class and it is our base or parent 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 Employee class and this class is extending from the base class, for this class we can say that it is a derived or child  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 Python Method Overriding.

 

 

 

 

 

Now run the code and this will be the result, and you can see that we have different implementation for the same method in our two classes, our base class and our derived class, even though they have the same names.

Python Method Overriding
Python Method Overriding

 

 

 

Leave a Comment

Share via
Copy link
Powered by Social Snap