{"id":2660,"date":"2019-07-17T11:14:38","date_gmt":"2019-07-17T11:14:38","guid":{"rendered":"https:\/\/prwatech.in\/blog\/?p=2660"},"modified":"2024-03-27T06:44:01","modified_gmt":"2024-03-27T06:44:01","slug":"python-inheritance-tutorial","status":"publish","type":"post","link":"https:\/\/prwatech.in\/blog\/python\/python-oops\/python-inheritance-tutorial\/","title":{"rendered":"Python Inheritance Tutorial"},"content":{"rendered":"<h1>Python Inheritance Tutorial<\/h1>\n<p>&nbsp;<\/p>\n<p><strong>Python Inheritance Tutorial<\/strong>, In this tutorial, we will learn What is inheritance in Python and Types of inheritance in Python. Here, You will also learn Python inheritance Syntax, an example of python inheritance which is helpful for any <a href=\"https:\/\/prwatech.in\/python-training-institute-in-bangalore\/\" target=\"_blank\" rel=\"noopener noreferrer\">Python developers<\/a>. Are you looking for the information on Python inheritance tutorial or Are you dreaming to become to certified Pro Python Developer, then stop just dreaming, get your <a href=\"https:\/\/prwatech.in\/python-training-institute-in-bangalore\/\" target=\"_blank\" rel=\"noopener noreferrer\">online python course with certificate<\/a>\u00a0from India\u2019s Leading <a href=\"https:\/\/prwatech.in\/python-training-institute-in-bangalore\/\" target=\"_blank\" rel=\"noopener noreferrer\">Python training institute<\/a>.<\/p>\n<p>Inheritance provides reusability &amp; supports transitivity. It offers faster development time, easier maintenance, and easy to extend and represents real world relationships. Do you want to know about what is inheritance in Python and Types of Inheritance in Python, then just follow the below mentioned Python Inheritance Tutorial for Beginners from <a href=\"https:\/\/prwatech.com\/\" target=\"_blank\" rel=\"noopener noreferrer\">Prwatech<\/a> and take an advanced <a href=\"https:\/\/prwatech.in\/python-training-institute-in-bangalore\/\" target=\"_blank\" rel=\"noopener noreferrer\">online course to learn python<\/a>\u00a0like a Pro from today itself under 10+ years of hands-on experienced Professionals.<\/p>\n<p>To get a better understanding of this concept, let&#8217;s start Inheritance in Python Tutorial.<\/p>\n<h2>What is Inheritance in Python?<\/h2>\n<p>Inheritance is the ability of one class to run or inherit the properties from another class. Inheritance is the fundamental feature of OOPs that varieties the functionality of the current class by adding new features. The benefits of inheritance are:<\/p>\n<p>It represents real-world relationships well. We don\u2019t have to write the same code again and again. Also, it allows us to add more features to a class without modifying it. Mean the reusability of code is possible.<\/p>\n<p>It is transitive in nature, which means that if class B inherits from another class A, then all the subclasses of B would automatically inherit from class A. Inheritance is used to define a new class having all the properties of the old existing class.<\/p>\n<p>The parent class is the class being inherited from, also called the superclass or the base class. The child class is the class that inherits from another class, also called subclass, or derived class.<\/p>\n<h3><strong>Create a Child Class<\/strong><\/h3>\n<p>A child class needs to identify its parent class. This can be done by writing the parent class name in the definition of the child&#8217;s class.<\/p>\n<p><strong>Syntax<\/strong><\/p>\n<p>classclassName(parentClassName):<\/p>\n<p><strong>Example<\/strong><\/p>\n<p>class xyz:<\/p>\n<p>print(&#8216;xyz is parent class&#8217;)<\/p>\n<p>classabc(xyz):<\/p>\n<p>print(&#8216;abc is child class of xyz&#8217;)<\/p>\n<p><strong>Output<\/strong><strong>:<\/strong><\/p>\n<p>xyz is parent class<\/p>\n<p>abc is child class of xyz<\/p>\n<p>Here abc is the child class or subclass and xyz is the parent class or superclass. Now let\u2019s see one more example which will show how the attributes and functions can be inherited by child class from the parent class.<\/p>\n<h2>Example of Python inheritance<\/h2>\n<p>Let\u2019s consider a \u2018Sports\u2019 category is parent class and \u2018Shoes\u2019 is child or subclass in following example.<\/p>\n<p><strong>class Sports:<\/strong><\/p>\n<p>def __init__(self, model, color, cost):<\/p>\n<p>self.__model = model<\/p>\n<p>self.__color = color<\/p>\n<p>self.__cost = cost<\/p>\n<p># __model,__color,__costt are private to Sports class<\/p>\n<p>defgetModel(self):<\/p>\n<p># It is accessible outside the class Sports<\/p>\n<p>returnself.__model<\/p>\n<p>defgetcolor(self):<\/p>\n<p>return self.__color<\/p>\n<p>defsetcost(self, color):<\/p>\n<p>self.__color = color<\/p>\n<p>defgetcost(self):<\/p>\n<p>returnself.__cost<\/p>\n<p>defsetcost(self, cost):<\/p>\n<p>self.__cost = cost<\/p>\n<p>class Shoes(Sports):<\/p>\n<p>def __init__(self, model,color,cost,brand):<\/p>\n<p># We have to call parent constructor to set parameters excluding brand.<\/p>\n<p>super().__init__(model, color, cost)<\/p>\n<p>self.__brand = brand<\/p>\n<p>defShoesinfo(self):<\/p>\n<p>returnself.getModel() + &#8221; of brand &#8220;+ self.__brand + &#8221; in &#8221; + self.getcolor() + &#8221; color for &#8221; + self.getcost() + &#8221; &#8221;<\/p>\n<p># In method getInfo we can call getmodel(), getcolor(),getcost() as they are accessible in the child class through inheritance.<\/p>\n<p>sh1 = Shoes(&#8220;Shoes&#8221;, &#8220;Black&#8221;, &#8220;Rs.3699&#8243;,&#8221;Xyz123&#8221;)<\/p>\n<p>print(sh1.Shoesinfo())<\/p>\n<p>print(sh1.getModel())<\/p>\n<p>sh2= Shoes(&#8220;Shoes&#8221;, &#8220;Black-red&#8221;, &#8220;Rs.4599&#8221;, &#8220;Xyz345&#8221;)<\/p>\n<p>print(sh2.Shoesinfo())<\/p>\n<h3><strong>Output<\/strong>:<\/h3>\n<p>Shoes of brand Xyz123 in Black color for Rs.3699<\/p>\n<p>Shoes of brand Xyz345 in Black-red color for Rs.4599.<\/p>\n<h3>Use of Super() :<\/h3>\n<p>The super() method permits to access the inherited methods which link to a class object. In the above example, we\u2019ve used the super() method in the constructor of the subclass i.e. child class <strong>\u2018Shoes\u2019<\/strong>. It is appealing to the function of the base class <strong>\u2018Sports\u2019.<\/strong><\/p>\n<h3>Working of super():<\/h3>\n<p>As we know in the above example subclass \u2018Shoes\u2019 has no properties defined such as color, cost, and model. But these are available in \u2018Sports\u2019. We can access those properties for child class Shoes with help of the super() function. Shoes have no method getModel() but it is accessible via Shoe class.<\/p>\n<h3><strong>Types of Inheritance in python<\/strong><\/h3>\n<p><strong>Single inheritance: <\/strong>If a subclass inherits from only one superclass, it is called single inheritance. (As stated in the above example)<\/p>\n<p><strong>Multiple inheritances:<\/strong> When a subclass inherits from multiple parent classes, it is called as multiple inheritances.Python has provisions for multiple inheritances. We have to specify all parent classes in the bracket as a list with comma separation.<\/p>\n<h3>Multiple inheritance Example<\/h3>\n<p>class super1(object):<\/p>\n<p>def __init__(self):<\/p>\n<p>self.test1 = &#8220;Python&#8221;<\/p>\n<p>print (&#8220;Superclass1&#8221;)<\/p>\n<p>class super2(object):<\/p>\n<p>def __init__(self):<\/p>\n<p>self.test2 = &#8220;Tutorials&#8221;<\/p>\n<p>print (&#8220;Superclass2&#8221;)<\/p>\n<p>classmultiinher(super1, super2):<\/p>\n<p>def __init__(self):<\/p>\n<p># Calling constructors of super1 and super2 classes<\/p>\n<p>super1.__init__(self)<\/p>\n<p>super2.__init__(self)<\/p>\n<p>print (&#8220;Derived&#8221;)<\/p>\n<p>defprintvals(self):<\/p>\n<p>print(self.test1, self.test2)<\/p>\n<p>obj = multiinher()<\/p>\n<p>obj.printvals()<\/p>\n<h3><strong>Output<\/strong><\/h3>\n<p>Superclass1<\/p>\n<p>Superclass2<\/p>\n<p>Derived<\/p>\n<p>Python Tutorials<\/p>\n<p>We hope you understand Python Inheritance Tutorial concepts. Get success in your career as a <a href=\"https:\/\/prwatech.in\/python-training-institute-in-bangalore\/\" target=\"_blank\" rel=\"noopener noreferrer\">Python developer<\/a> by being a part of the <a href=\"https:\/\/prwatech.com\/\" target=\"_blank\" rel=\"noopener noreferrer\">Prwatech<\/a>, India&#8217;s leading <a href=\"https:\/\/prwatech.in\/python-training-institute-in-bangalore\/\" target=\"_blank\" rel=\"noopener noreferrer\">Python training institute in Bangalore<\/a>.<\/p>\n<p><iframe loading=\"lazy\" src=\"https:\/\/www.youtube.com\/embed\/QjYfZk6IQCc\" width=\"850\" height=\"315\" frameborder=\"0\" allowfullscreen=\"allowfullscreen\"><\/iframe><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Python Inheritance Tutorial &nbsp; Python Inheritance Tutorial, In this tutorial, we will learn What is inheritance in Python and Types of inheritance in Python. Here, You will also learn Python inheritance Syntax, an example of python inheritance which is helpful for any Python developers. Are you looking for the information on Python inheritance tutorial or [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":3387,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[28,1679],"tags":[539,546,542,74],"class_list":["post-2660","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-python","category-python-oops","tag-online-course-on-python","tag-online-python-programming-course","tag-online-python-training-course","tag-python-inheritance-tutorial"],"acf":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v25.7 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Python Inheritance Tutorial | Inheritance In Python with Examples<\/title>\n<meta name=\"description\" content=\"Here is the detailed information of Python Inheritance Tutorial with Examples. Learn types of inheritance in Python from Prwatech Today.\" \/>\n<meta name=\"robots\" content=\"noindex, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Python Inheritance Tutorial | Inheritance In Python with Examples\" \/>\n<meta property=\"og:description\" content=\"Here is the detailed information of Python Inheritance Tutorial with Examples. Learn types of inheritance in Python from Prwatech Today.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/prwatech.in\/blog\/python\/python-oops\/python-inheritance-tutorial\/\" \/>\n<meta property=\"og:site_name\" content=\"Prwatech\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/prwatech.in\/\" \/>\n<meta property=\"article:published_time\" content=\"2019-07-17T11:14:38+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-03-27T06:44:01+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2019\/07\/Python-Inheritance-Tutorial.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"960\" \/>\n\t<meta property=\"og:image:height\" content=\"550\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"Prwatech\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@Eduprwatech\" \/>\n<meta name=\"twitter:site\" content=\"@Eduprwatech\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Prwatech\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"4 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/prwatech.in\/blog\/python\/python-oops\/python-inheritance-tutorial\/\",\"url\":\"https:\/\/prwatech.in\/blog\/python\/python-oops\/python-inheritance-tutorial\/\",\"name\":\"Python Inheritance Tutorial | Inheritance In Python with Examples\",\"isPartOf\":{\"@id\":\"https:\/\/prwatech.in\/blog\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/prwatech.in\/blog\/python\/python-oops\/python-inheritance-tutorial\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/prwatech.in\/blog\/python\/python-oops\/python-inheritance-tutorial\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2019\/07\/Python-Inheritance-Tutorial.jpg\",\"datePublished\":\"2019-07-17T11:14:38+00:00\",\"dateModified\":\"2024-03-27T06:44:01+00:00\",\"author\":{\"@id\":\"https:\/\/prwatech.in\/blog\/#\/schema\/person\/db90baff7744090b2288bbc98fea87f3\"},\"description\":\"Here is the detailed information of Python Inheritance Tutorial with Examples. Learn types of inheritance in Python from Prwatech Today.\",\"breadcrumb\":{\"@id\":\"https:\/\/prwatech.in\/blog\/python\/python-oops\/python-inheritance-tutorial\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/prwatech.in\/blog\/python\/python-oops\/python-inheritance-tutorial\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/prwatech.in\/blog\/python\/python-oops\/python-inheritance-tutorial\/#primaryimage\",\"url\":\"https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2019\/07\/Python-Inheritance-Tutorial.jpg\",\"contentUrl\":\"https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2019\/07\/Python-Inheritance-Tutorial.jpg\",\"width\":960,\"height\":550,\"caption\":\"Python Inheritance Tutorial\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/prwatech.in\/blog\/python\/python-oops\/python-inheritance-tutorial\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/prwatech.in\/blog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Python Inheritance Tutorial\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/prwatech.in\/blog\/#website\",\"url\":\"https:\/\/prwatech.in\/blog\/\",\"name\":\"Prwatech\",\"description\":\"Share Ideas, Start Something Good.\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/prwatech.in\/blog\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/prwatech.in\/blog\/#\/schema\/person\/db90baff7744090b2288bbc98fea87f3\",\"name\":\"Prwatech\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/prwatech.in\/blog\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/c00bafc1b04045f31eda917de39891456c44fa47c092b9bb6be0f860a3a30a2f?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/c00bafc1b04045f31eda917de39891456c44fa47c092b9bb6be0f860a3a30a2f?s=96&d=mm&r=g\",\"caption\":\"Prwatech\"},\"url\":\"https:\/\/prwatech.in\/blog\/author\/prwatech123\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Python Inheritance Tutorial | Inheritance In Python with Examples","description":"Here is the detailed information of Python Inheritance Tutorial with Examples. Learn types of inheritance in Python from Prwatech Today.","robots":{"index":"noindex","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"og_locale":"en_US","og_type":"article","og_title":"Python Inheritance Tutorial | Inheritance In Python with Examples","og_description":"Here is the detailed information of Python Inheritance Tutorial with Examples. Learn types of inheritance in Python from Prwatech Today.","og_url":"https:\/\/prwatech.in\/blog\/python\/python-oops\/python-inheritance-tutorial\/","og_site_name":"Prwatech","article_publisher":"https:\/\/www.facebook.com\/prwatech.in\/","article_published_time":"2019-07-17T11:14:38+00:00","article_modified_time":"2024-03-27T06:44:01+00:00","og_image":[{"width":960,"height":550,"url":"https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2019\/07\/Python-Inheritance-Tutorial.jpg","type":"image\/jpeg"}],"author":"Prwatech","twitter_card":"summary_large_image","twitter_creator":"@Eduprwatech","twitter_site":"@Eduprwatech","twitter_misc":{"Written by":"Prwatech","Est. reading time":"4 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/prwatech.in\/blog\/python\/python-oops\/python-inheritance-tutorial\/","url":"https:\/\/prwatech.in\/blog\/python\/python-oops\/python-inheritance-tutorial\/","name":"Python Inheritance Tutorial | Inheritance In Python with Examples","isPartOf":{"@id":"https:\/\/prwatech.in\/blog\/#website"},"primaryImageOfPage":{"@id":"https:\/\/prwatech.in\/blog\/python\/python-oops\/python-inheritance-tutorial\/#primaryimage"},"image":{"@id":"https:\/\/prwatech.in\/blog\/python\/python-oops\/python-inheritance-tutorial\/#primaryimage"},"thumbnailUrl":"https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2019\/07\/Python-Inheritance-Tutorial.jpg","datePublished":"2019-07-17T11:14:38+00:00","dateModified":"2024-03-27T06:44:01+00:00","author":{"@id":"https:\/\/prwatech.in\/blog\/#\/schema\/person\/db90baff7744090b2288bbc98fea87f3"},"description":"Here is the detailed information of Python Inheritance Tutorial with Examples. Learn types of inheritance in Python from Prwatech Today.","breadcrumb":{"@id":"https:\/\/prwatech.in\/blog\/python\/python-oops\/python-inheritance-tutorial\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/prwatech.in\/blog\/python\/python-oops\/python-inheritance-tutorial\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/prwatech.in\/blog\/python\/python-oops\/python-inheritance-tutorial\/#primaryimage","url":"https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2019\/07\/Python-Inheritance-Tutorial.jpg","contentUrl":"https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2019\/07\/Python-Inheritance-Tutorial.jpg","width":960,"height":550,"caption":"Python Inheritance Tutorial"},{"@type":"BreadcrumbList","@id":"https:\/\/prwatech.in\/blog\/python\/python-oops\/python-inheritance-tutorial\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/prwatech.in\/blog\/"},{"@type":"ListItem","position":2,"name":"Python Inheritance Tutorial"}]},{"@type":"WebSite","@id":"https:\/\/prwatech.in\/blog\/#website","url":"https:\/\/prwatech.in\/blog\/","name":"Prwatech","description":"Share Ideas, Start Something Good.","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/prwatech.in\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/prwatech.in\/blog\/#\/schema\/person\/db90baff7744090b2288bbc98fea87f3","name":"Prwatech","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/prwatech.in\/blog\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/c00bafc1b04045f31eda917de39891456c44fa47c092b9bb6be0f860a3a30a2f?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/c00bafc1b04045f31eda917de39891456c44fa47c092b9bb6be0f860a3a30a2f?s=96&d=mm&r=g","caption":"Prwatech"},"url":"https:\/\/prwatech.in\/blog\/author\/prwatech123\/"}]}},"_links":{"self":[{"href":"https:\/\/prwatech.in\/blog\/wp-json\/wp\/v2\/posts\/2660","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/prwatech.in\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/prwatech.in\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/prwatech.in\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/prwatech.in\/blog\/wp-json\/wp\/v2\/comments?post=2660"}],"version-history":[{"count":14,"href":"https:\/\/prwatech.in\/blog\/wp-json\/wp\/v2\/posts\/2660\/revisions"}],"predecessor-version":[{"id":11121,"href":"https:\/\/prwatech.in\/blog\/wp-json\/wp\/v2\/posts\/2660\/revisions\/11121"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/prwatech.in\/blog\/wp-json\/wp\/v2\/media\/3387"}],"wp:attachment":[{"href":"https:\/\/prwatech.in\/blog\/wp-json\/wp\/v2\/media?parent=2660"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/prwatech.in\/blog\/wp-json\/wp\/v2\/categories?post=2660"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/prwatech.in\/blog\/wp-json\/wp\/v2\/tags?post=2660"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}