{"id":3910,"date":"2022-12-05T07:54:27","date_gmt":"2022-12-05T07:54:27","guid":{"rendered":"https:\/\/gyanipandit.com\/programming\/?p=3910"},"modified":"2022-12-05T07:54:29","modified_gmt":"2022-12-05T07:54:29","slug":"method-overriding-in-python","status":"publish","type":"post","link":"https:\/\/gyanipandit.com\/programming\/method-overriding-in-python\/","title":{"rendered":"Method Overriding in Python With Example"},"content":{"rendered":"<p style=\"text-align: center;\">Method overriding in Python<\/p>\n<p>Now, we are going to have a quite detailed look at another interesting concept, called the Method overriding. This is a very important feature of inheritance. Let\u2019s consider an example over here so that we can understand the concept of Method overriding in a better way.<\/p>\n<p>Let\u2019s consider that we have an Animal class. It has some behaviors and attributes which are there in most animals, like walking, eating, sleeping, etc. Now, the thing is that there are many different types of animals, like lions, dogs, Sheep, Human (Yes, humans are animals). Now, all these animals also have basic behaviors, like sleeping, eating, walking, running, etc., but they can also have their own implementation for that behaviors. For example, sheep are herbivore, which means that it eats grass, plants, etc&#8230;\u00a0 On the other hand, Lions are Carnivores<\/p>\n<h2 style=\"text-align: center;\">Method Overriding in Python<\/h2>\n<p>So, the point is that the lion has inherited the behavior of eating from the Animal class, but it has implemented it in its own way. If you feel confused, do not even worry about it, because most of the things should get clear from the example, that we are going to consider now.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-3911 size-full\" src=\"https:\/\/gyanipandit.com\/programming\/wp-content\/uploads\/2022\/11\/METHOD-OVERRIDING.jpg\" alt=\"\" width=\"639\" height=\"199\" srcset=\"https:\/\/gyanipandit.com\/programming\/wp-content\/uploads\/2022\/11\/METHOD-OVERRIDING.jpg 639w, https:\/\/gyanipandit.com\/programming\/wp-content\/uploads\/2022\/11\/METHOD-OVERRIDING-300x93.jpg 300w\" sizes=\"auto, (max-width: 639px) 100vw, 639px\" \/><\/p>\n<p>From the above example, we can simply know that the Lion class inherits the Animal class, so, it has got the behaviors and\/or attributes from the Animal class in this case. Now, the thing is that if we try to create the Lion object, and then try to implement the eat method on the lion object, we do not have any problem, because the eat method is available to the lion object because of the inheritance.<\/p>\n<p>But here, we are now going to redefine the method eat, for the lion, which will have the implementation of the eat method for the lion. Have a look at the below example now &#8211;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-3912 size-full\" src=\"https:\/\/gyanipandit.com\/programming\/wp-content\/uploads\/2022\/11\/METHOD-OVERRIDING-1.jpg\" alt=\"\" width=\"643\" height=\"243\" srcset=\"https:\/\/gyanipandit.com\/programming\/wp-content\/uploads\/2022\/11\/METHOD-OVERRIDING-1.jpg 643w, https:\/\/gyanipandit.com\/programming\/wp-content\/uploads\/2022\/11\/METHOD-OVERRIDING-1-300x113.jpg 300w\" sizes=\"auto, (max-width: 643px) 100vw, 643px\" \/><\/p>\n<p>As you can see, now, in the Lion class as well, we have redefined the method eat, so, we are overriding the method eat here. The concept is called method overriding, and the method is called the overriding method.<\/p>\n<p>You can simply see that the eat method was already provided for the lion class due to inheritance, but we redefined the eat method in the Lion class. So, if we are writing the method in the subclass, with the same name, same parameters, and return types (if applicable) as from the superclass, then we can say that the method in the subclass overrides the method in the superclass.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Method overriding in Python Now, we are going to have a quite detailed look at another interesting concept, called the Method overriding. This is a very important feature of inheritance. Let\u2019s consider an example over here so that we can understand the concept of Method overriding in a better way. Let\u2019s consider that we have [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[25],"tags":[26,27],"class_list":{"0":"post-3910","1":"post","2":"type-post","3":"status-publish","4":"format-standard","6":"category-python-tutorial","7":"tag-python","8":"tag-python-tutorial"},"_links":{"self":[{"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/posts\/3910","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/comments?post=3910"}],"version-history":[{"count":2,"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/posts\/3910\/revisions"}],"predecessor-version":[{"id":4245,"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/posts\/3910\/revisions\/4245"}],"wp:attachment":[{"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/media?parent=3910"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/categories?post=3910"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/tags?post=3910"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}