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:

  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, and you don’t need to write it again. this will help in reducing code duplication and improving code maintainability.
  3. Polymorphism: Method overriding is an important feature of object oriented programming. 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. parent class can define a method as a template for child classes, and child classes can override this method to provide their own behavior.

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.

 

 

Learn More

 

 

 

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

 

 

 

FAQs:

 

What is overriding a method in Python?

Overriding a method in Python refers to the ability of a subclass to provide a specific implementation of a method that is already defined in its superclass. This allows the subclass to customize the behavior of the method according its own requirements.

 

 

Can you provide an example of method overriding?
Yes, for example we have a superclass Animal with a method make_sound(). Now, if we have a subclass Dog inheriting from Animal, we can override the make_sound() method in Dog class to make it bark, whereas in the superclass it might make a generic sound. 

 

 

What is the difference between overriding and overloading in Python? Overriding and overloading are both concepts in object-oriented programming, but they serve different purposes:

  • Method Overriding: Involves providing a new implementation for a method that already exists in a superclass. It is used for runtime polymorphism and to customize behavior in subclasses.
  • Method Overloading: Involves defining multiple methods with the same name but different parameter lists within the same class. Python does not support method overloading natively, but it can be achieved using default parameter values or variable-length argument lists. Overloading is used for compile-time polymorphism and to provide multiple ways to call a method with different argument types or counts.

Subscribe and Get Free Video Courses & Articles in your Email

 

Leave a Comment

Share via
Copy link
Powered by Social Snap
×