{"id":745,"date":"2019-12-10T06:38:50","date_gmt":"2019-12-10T06:38:50","guid":{"rendered":"https:\/\/www.askpython.com\/?p=745"},"modified":"2026-09-29T16:19:29","modified_gmt":"2026-09-29T16:19:29","slug":"inheritance-in-python","status":"publish","type":"post","link":"https:\/\/www.askpython.com\/python\/oops\/inheritance-in-python","title":{"rendered":"Inheritance in Python"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\">Inheritance at a glance!<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">In the world of Object-Oriented Programming (OOP), Inheritance refers to the mechanism of the capability of a class to derive or extend the properties from another class in the run. This property enables the derived class to acquire the properties or traits of the base class.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Inheritance is considered one of the most important aspects of OOP because it serves the feature of <strong>reusability<\/strong>, thus making the piece of code more reliable.<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"701\" height=\"341\" src=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2019\/12\/python-inheritance.png\" alt=\"Python Inheritance\" class=\"wp-image-1158\" srcset=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2019\/12\/python-inheritance.png 701w, https:\/\/www.askpython.com\/wp-content\/uploads\/2019\/12\/python-inheritance-300x146.png 300w\" sizes=\"auto, (max-width: 701px) 100vw, 701px\" \/><figcaption>Python Inheritance<\/figcaption><\/figure><\/div>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Benefits of Inheritance <\/h2>\n\n\n\n<ul class=\"wp-block-list\"><li>Inheritance depicts relationships that resemble real-world scenarios.<\/li><li>It provides the feature of re-usability which allows the user to add more features to the derived class without altering it.<\/li><li>If a class Y inherits from class X, then automatically all the sub-classes of Y would inherit from class X.<\/li><\/ul>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Basic Terminologies of Inheritance <\/h2>\n\n\n\n<ol class=\"wp-block-list\"><li><em>Subclass\/Derived class<\/em>: It is a class that inherits the properties from another class (usually the base class).<\/li><li><em>Superclass\/Base class<\/em>: It is the class from which other subclasses are derived.<\/li><li>A derived class usually <em>derives\/inherits\/extends<\/em> the base class.<\/li><\/ol>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Python Inheritance Syntax<\/h2>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nclass SuperClassName:\n  Body of Super class\n\n\nclass DerivedClass_Name(SuperClass):\n  Body of derived class\n<\/pre><\/div>\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Python Inheritance Example<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Let&#8217;s dive into the world of inheritance in Python with simple examples.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Step 1: Create a Base class<\/h3>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nclass Father:\n    # The keyword &#039;self&#039; is used to represent the instance of a class.\n    # By using the &quot;self&quot; keyword we access the attributes and methods of the class in python.\n    # The method &quot;__init__&quot;  is called as a constructor in object oriented terminology.\n    # This method is called when an object is created from a class.\n    # it allows the class to initialize the attributes of the class.\n    def __init__(self, name, lastname):\n        self.name = name\n        self.lastname = lastname\n\n    def printname(self):\n        print(self.name, self.lastname)\n\n\n# Use the Father class to create an object, and then execute the printname method:\n\nx = Father(&quot;Anees&quot;, &quot;Mulani&quot;)\nx.printname()\n\n<\/pre><\/div>\n\n\n<p class=\"wp-block-paragraph\"><strong>Output<\/strong>: Anees Mulani<\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">Step 2: Create a Derived class<\/h3>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\n# The subclass __init__() function overrides the inheritance of the base class __init__() function.\n\nclass Son(Father):\n    def __init__(self, name, lastname):\n        Father.__init__(self, name, lastname)\n\n\nx = Son(&quot;Dev&quot;, &quot;Bajaj&quot;)\nx.printname()\n<\/pre><\/div>\n\n\n<p class=\"wp-block-paragraph\"><strong>Output<\/strong>: Dev Bajaj<\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Use of super() function <\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">By using the <code>super()<\/code> function, you do not have to use the name of the parent element, it will automatically inherit the methods and properties from its parent.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nclass Father:\n  def __init__(self, name, lastname):\n    self.name = name\n    self.lastname = lastname\n\n  def printname(self):\n    print(self.name, self.lastname)\n\nclass Son(Father):\n  def __init__(self, name, lastname):\n    super().__init__(name, lastname)\n\nx = Student(&quot;Dev&quot;, &quot;Bajaj&quot;)\nx.printname()\n<\/pre><\/div>\n\n\n<p class=\"wp-block-paragraph\"><strong>Output<\/strong>: Dev Bajaj<\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">References:<\/h2>\n\n\n\n<ul class=\"wp-block-list\"><li>Python Inheritance on JournalDev<\/li><li><a href=\"https:\/\/docs.python.org\/3\/tutorial\/classes.html#inheritance\" target=\"_blank\" rel=\"noreferrer noopener\" aria-label=\"Python.org Docs (opens in a new tab)\">Python.org Docs<\/a><\/li><\/ul>\n","protected":false},"excerpt":{"rendered":"<p>Inheritance at a glance! In the world of Object-Oriented Programming (OOP), Inheritance refers to the mechanism of the capability of a class to derive or extend the properties from another class in the run. This property enables the derived class to acquire the properties or traits of the base class. Inheritance is considered one of [&hellip;]<\/p>\n","protected":false},"author":4,"featured_media":67431,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[11],"tags":[],"class_list":["post-745","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-oops"],"_links":{"self":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts\/745","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/users\/4"}],"replies":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/comments?post=745"}],"version-history":[{"count":0,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts\/745\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/media\/67431"}],"wp:attachment":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/media?parent=745"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/categories?post=745"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/tags?post=745"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}