{"id":2402,"date":"2023-01-31T16:53:18","date_gmt":"2023-01-31T16:53:18","guid":{"rendered":"https:\/\/blog.samarthya.me\/wps\/?p=2402"},"modified":"2023-01-31T16:53:20","modified_gmt":"2023-01-31T16:53:20","slug":"python-inheritance","status":"publish","type":"post","link":"https:\/\/blog.samarthya.me\/wps\/2023\/01\/31\/python-inheritance\/","title":{"rendered":"Python: Inheritance"},"content":{"rendered":"\n<p>Inheritance is a mechanism in object-oriented programming (OOP) that allows you to create new classes that are derived from existing classes. The derived class inherits attributes and behaviors from the base class, and can also have additional attributes and behaviors of its own.<\/p>\n\n\n\n<p>Here&#8217;s an example of inheritance in Python:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class Animal:\n    def __init__(self, name):\n        self.name = name\n\n    def make_sound(self):\n        print(\"Some sound\")\n\nclass Dog(Animal):\n    def __init__(self, name, breed):\n        super().__init__(name)\n        self.breed = breed\n\n    def make_sound(self):\n        print(\"Woof!\")<\/code><\/pre>\n\n\n\n<p>In this example, the <code>Animal<\/code> class is the base class and the <code>Dog<\/code> class is the derived class. The <code>Dog<\/code> class inherits the <code>name<\/code> attribute from the <code>Animal<\/code> class, and also has its own <code>breed<\/code> attribute.<\/p>\n\n\n\n<p>The <code>make_sound<\/code> method in the <code>Dog<\/code> class overrides the <code>make_sound<\/code> method in the <code>Animal<\/code> class. This is known as method overriding.<\/p>\n\n\n\n<p>The <code>super()<\/code> function is used to call the <code>__init__<\/code> method of the base class, allowing the derived class to inherit the attributes and behaviors of the base class.<\/p>\n\n\n\n<p>You can create instances of the <code>Dog<\/code> class just like any other class:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>dog = Dog(\"Mydo\", \"Belgian\")\ndog.make_sound()\n# Output: Woof!<\/code><\/pre>\n\n\n\n<p>This creates a new instance of the <code>Dog<\/code> class, with the <code>name<\/code> attribute set to &#8220;Mydo&#8221; and the <code>breed<\/code> attribute set to &#8220;Belgian&#8221;. The <code>make_sound<\/code> method is then called on the <code>dog<\/code> instance to produce the output &#8220;Woof!&#8221;.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Inheritance is a mechanism in object-oriented programming (OOP) that allows you to create new classes that are derived from existing classes. The derived class inherits attributes and behaviors from the base class, and can also have additional attributes and behaviors of its own. Here&#8217;s an example of inheritance in Python: In this example, the Animal [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":2399,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"image","meta":{"_exactmetrics_skip_tracking":false,"_exactmetrics_sitenote_active":false,"_exactmetrics_sitenote_note":"","_exactmetrics_sitenote_category":0,"footnotes":""},"categories":[34],"tags":[270],"class_list":["post-2402","post","type-post","status-publish","format-image","has-post-thumbnail","hentry","category-technical","tag-python","post_format-post-format-image"],"_links":{"self":[{"href":"https:\/\/blog.samarthya.me\/wps\/wp-json\/wp\/v2\/posts\/2402","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.samarthya.me\/wps\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.samarthya.me\/wps\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.samarthya.me\/wps\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.samarthya.me\/wps\/wp-json\/wp\/v2\/comments?post=2402"}],"version-history":[{"count":1,"href":"https:\/\/blog.samarthya.me\/wps\/wp-json\/wp\/v2\/posts\/2402\/revisions"}],"predecessor-version":[{"id":2403,"href":"https:\/\/blog.samarthya.me\/wps\/wp-json\/wp\/v2\/posts\/2402\/revisions\/2403"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/blog.samarthya.me\/wps\/wp-json\/wp\/v2\/media\/2399"}],"wp:attachment":[{"href":"https:\/\/blog.samarthya.me\/wps\/wp-json\/wp\/v2\/media?parent=2402"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.samarthya.me\/wps\/wp-json\/wp\/v2\/categories?post=2402"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.samarthya.me\/wps\/wp-json\/wp\/v2\/tags?post=2402"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}