{"id":109,"date":"2026-03-02T08:29:28","date_gmt":"2026-03-02T03:29:28","guid":{"rendered":"https:\/\/gigz.pk\/python\/?post_type=lesson&#038;p=109"},"modified":"2026-03-15T17:18:21","modified_gmt":"2026-03-15T12:18:21","slug":"inheritance","status":"publish","type":"lesson","link":"https:\/\/gigz.pk\/python\/lesson\/inheritance\/","title":{"rendered":"\u00a0Inheritance"},"content":{"rendered":"\n<p>Inheritance is one of the main concepts of Object-Oriented Programming (OOP).<\/p>\n\n\n\n<p>Inheritance allows one class to use the properties and methods of another class.<\/p>\n\n\n\n<p>In simple words:<br>Inheritance means creating a new class from an existing class.<\/p>\n\n\n\n<p>The existing class is called the <strong>Parent Class (Base Class)<\/strong>.<br>The new class is called the <strong>Child Class (Derived Class)<\/strong>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Why Use Inheritance?<\/strong><\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Code reuse<\/li>\n\n\n\n<li>Reduces duplication<\/li>\n\n\n\n<li>Improves maintainability<\/li>\n\n\n\n<li>Supports hierarchical structure<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Basic Syntax<\/strong><\/h2>\n\n\n\n<pre class=\"wp-block-preformatted\">class Parent:<br>    # parent class codeclass Child(Parent):<br>    # child class code<\/pre>\n\n\n\n<p>The child class automatically gets access to the parent class methods and variables.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Simple Example<\/strong><\/h2>\n\n\n\n<pre class=\"wp-block-preformatted\">class Person:<br>    def greet(self):<br>        print(\"Hello\")class Student(Person):<br>    def study(self):<br>        print(\"Studying\")s1 = Student()s1.greet()<br>s1.study()<\/pre>\n\n\n\n<p>Output:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">Hello<br>Studying<\/pre>\n\n\n\n<p>Here:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>Student<\/code> inherits from <code>Person<\/code><\/li>\n\n\n\n<li><code>Student<\/code> can use <code>greet()<\/code> method of <code>Person<\/code><\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Using Constructor in Inheritance<\/strong><\/h2>\n\n\n\n<p>If the parent class has a constructor, we can call it using <code>super()<\/code>.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">class Person:<br>    def __init__(self, name):<br>        self.name = nameclass Student(Person):<br>    def __init__(self, name, grade):<br>        super().__init__(name)<br>        self.grade = grades1 = Student(\"Hira\", \"A\")print(s1.name)<br>print(s1.grade)<\/pre>\n\n\n\n<p><code>super()<\/code> calls the parent class constructor.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Types of Inheritance<\/strong><\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">1. Single Inheritance<\/h3>\n\n\n\n<p>One child class inherits from one parent class.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">2. Multiple Inheritance<\/h3>\n\n\n\n<p>One child class inherits from multiple parent classes.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">class Father:<br>    def skill1(self):<br>        print(\"Gardening\")class Mother:<br>    def skill2(self):<br>        print(\"Cooking\")class Child(Father, Mother):<br>    passc1 = Child()<br>c1.skill1()<br>c1.skill2()<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">3. Multilevel Inheritance<\/h3>\n\n\n\n<p>A class inherits from a class that already inherited from another class.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">4. Hierarchical Inheritance<\/h3>\n\n\n\n<p>Multiple child classes inherit from one parent class.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Method Overriding<\/strong><\/h2>\n\n\n\n<p>A child class can redefine a parent method.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">class Animal:<br>    def sound(self):<br>        print(\"Some sound\")class Dog(Animal):<br>    def sound(self):<br>        print(\"Bark\")d1 = Dog()<br>d1.sound()<\/pre>\n\n\n\n<p>Output:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">Bark<\/pre>\n\n\n\n<p>The child method overrides the parent method.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Key Takeaway<\/strong><\/h2>\n\n\n\n<p>Inheritance allows a class to reuse code from another class.<br>It promotes code reusability, structure, and better organization in OOP programs.<\/p>\n\n\n<div class=\"yoast-breadcrumbs\"><span><span><a href=\"https:\/\/gigz.pk\/python\/\">Home<\/a><\/span> \u00bb <span class=\"breadcrumb_last\" aria-current=\"page\">OBJECT ORIENTED PROGRAMMING IN PYTHON (OOP) > OOP Concepts > Inheritance<\/span><\/span><\/div>\n\n\n<div class=\"schema-faq wp-block-yoast-faq-block\"><div class=\"schema-faq-section\" id=\"faq-question-1773577243930\"><strong class=\"schema-faq-question\"><\/strong> <p class=\"schema-faq-answer\"><\/p> <\/div> <\/div>\n","protected":false},"menu_order":52,"template":"","class_list":["post-109","lesson","type-lesson","status-publish","hentry"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.5 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>\u00a0Inheritance - One Language. Endless Possibilities<\/title>\n<meta name=\"description\" content=\"Learn inheritance in Python OOP, parent and child classes, super(), method overriding, and types of inheritance with examples.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/gigz.pk\/python\/lesson\/inheritance\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"\u00a0Inheritance - One Language. Endless Possibilities\" \/>\n<meta property=\"og:description\" content=\"Learn inheritance in Python OOP, parent and child classes, super(), method overriding, and types of inheritance with examples.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/gigz.pk\/python\/lesson\/inheritance\/\" \/>\n<meta property=\"og:site_name\" content=\"One Language. Endless Possibilities\" \/>\n<meta property=\"article:modified_time\" content=\"2026-03-15T12:18:21+00:00\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data1\" content=\"1 minute\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":[\"WebPage\",\"FAQPage\"],\"@id\":\"https:\\\/\\\/gigz.pk\\\/python\\\/lesson\\\/inheritance\\\/\",\"url\":\"https:\\\/\\\/gigz.pk\\\/python\\\/lesson\\\/inheritance\\\/\",\"name\":\"\u00a0Inheritance - One Language. Endless Possibilities\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/gigz.pk\\\/python\\\/#website\"},\"datePublished\":\"2026-03-02T03:29:28+00:00\",\"dateModified\":\"2026-03-15T12:18:21+00:00\",\"description\":\"Learn inheritance in Python OOP, parent and child classes, super(), method overriding, and types of inheritance with examples.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/gigz.pk\\\/python\\\/lesson\\\/inheritance\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/gigz.pk\\\/python\\\/lesson\\\/inheritance\\\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/gigz.pk\\\/python\\\/lesson\\\/inheritance\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/gigz.pk\\\/python\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"OBJECT ORIENTED PROGRAMMING IN PYTHON (OOP) > OOP Concepts > Inheritance\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/gigz.pk\\\/python\\\/#website\",\"url\":\"https:\\\/\\\/gigz.pk\\\/python\\\/\",\"name\":\"One Language. Endless Possibilities\",\"description\":\"\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/gigz.pk\\\/python\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"\u00a0Inheritance - One Language. Endless Possibilities","description":"Learn inheritance in Python OOP, parent and child classes, super(), method overriding, and types of inheritance with examples.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/gigz.pk\/python\/lesson\/inheritance\/","og_locale":"en_US","og_type":"article","og_title":"\u00a0Inheritance - One Language. Endless Possibilities","og_description":"Learn inheritance in Python OOP, parent and child classes, super(), method overriding, and types of inheritance with examples.","og_url":"https:\/\/gigz.pk\/python\/lesson\/inheritance\/","og_site_name":"One Language. Endless Possibilities","article_modified_time":"2026-03-15T12:18:21+00:00","twitter_card":"summary_large_image","twitter_misc":{"Est. reading time":"1 minute"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":["WebPage","FAQPage"],"@id":"https:\/\/gigz.pk\/python\/lesson\/inheritance\/","url":"https:\/\/gigz.pk\/python\/lesson\/inheritance\/","name":"\u00a0Inheritance - One Language. Endless Possibilities","isPartOf":{"@id":"https:\/\/gigz.pk\/python\/#website"},"datePublished":"2026-03-02T03:29:28+00:00","dateModified":"2026-03-15T12:18:21+00:00","description":"Learn inheritance in Python OOP, parent and child classes, super(), method overriding, and types of inheritance with examples.","breadcrumb":{"@id":"https:\/\/gigz.pk\/python\/lesson\/inheritance\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/gigz.pk\/python\/lesson\/inheritance\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/gigz.pk\/python\/lesson\/inheritance\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/gigz.pk\/python\/"},{"@type":"ListItem","position":2,"name":"OBJECT ORIENTED PROGRAMMING IN PYTHON (OOP) > OOP Concepts > Inheritance"}]},{"@type":"WebSite","@id":"https:\/\/gigz.pk\/python\/#website","url":"https:\/\/gigz.pk\/python\/","name":"One Language. Endless Possibilities","description":"","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/gigz.pk\/python\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"}]}},"_links":{"self":[{"href":"https:\/\/gigz.pk\/python\/wp-json\/wp\/v2\/lesson\/109","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/gigz.pk\/python\/wp-json\/wp\/v2\/lesson"}],"about":[{"href":"https:\/\/gigz.pk\/python\/wp-json\/wp\/v2\/types\/lesson"}],"wp:attachment":[{"href":"https:\/\/gigz.pk\/python\/wp-json\/wp\/v2\/media?parent=109"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}