

{"id":125742,"date":"2023-11-09T17:14:51","date_gmt":"2023-11-09T11:44:51","guid":{"rendered":"https:\/\/data-flair.training\/blogs\/?p=125742"},"modified":"2024-02-28T12:15:15","modified_gmt":"2024-02-28T06:45:15","slug":"python-program-on-constructor-role-in-inheritance","status":"publish","type":"post","link":"https:\/\/data-flair.training\/blogs\/python-program-on-constructor-role-in-inheritance\/","title":{"rendered":"Python Program on Constructor Role in Inheritance"},"content":{"rendered":"<p>In the realm of object-oriented programming, inheritance stands as a powerful mechanism, enabling the creation of new classes based on existing ones. Python, known for its simplicity and versatility, employs constructors to facilitate the inheritance process. This article delves into the provided code, shedding light on the role of constructors in inheritance.<\/p>\n<p>Specifically, we explore how a derived class, &#8216;Marks,&#8217; inherits attributes from its base class, &#8216;Student,&#8217; and employs a constructor to initialize its unique attributes, showcasing the seamless integration of inheritance and constructors in Python.<\/p>\n<h2>Prerequisites<\/h2>\n<ul>\n<li>Object-oriented programming (OOP) concepts<\/li>\n<li>Python syntax and class structure<\/li>\n<li>Inheritance in Python<\/li>\n<\/ul>\n<h3>Topic Explanation<\/h3>\n<p>The code introduces two classes, &#8216;Student&#8217; and &#8216;Marks,&#8217; showcasing the concept of inheritance. The &#8216;Student&#8217; class acts as the base class, featuring an &#8216;init&#8217; method that initializes three instance variables: &#8216;rno&#8217; (roll number), &#8216;name,&#8217; and &#8216;course.&#8217; Subsequently, the &#8216;Marks&#8217; class, which inherits from &#8216;Student,&#8217; employs its own &#8216;init&#8217; method. Crucially, it utilizes &#8216;super().init()&#8217; to invoke the constructor of the base class, ensuring that the inherited attributes are appropriately initialized.<\/p>\n<p>The &#8216;Marks&#8217; class introduces a new attribute, &#8216;per&#8217; (percentage), showcasing how a derived class can extend the functionality of its base class. The &#8216;display&#8217; method in &#8216;Marks&#8217; then prints both the inherited attributes (&#8216;rno,&#8217; &#8216;name,&#8217; &#8216;course&#8217;) and the unique attribute (&#8216;per&#8217;). The execution of &#8216;M1 = Marks()&#8217; demonstrates the inheritance chain, where the constructor of the &#8216;Student&#8217; class is implicitly called through &#8216;super().init(),&#8217; and the &#8216;display&#8217; method prints the combined attributes. This exemplifies the synergy between constructors and inheritance in Python, providing a streamlined way to manage attributes across class hierarchies.<\/p>\n<h4>Code:<\/h4>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\"># Role of Constructor in Inheritance\r\nclass Student:\r\n    def __init__(self):\r\n        # Constructor of the base class 'Student' initializing instance variables\r\n        self.rno = 100\r\n        self.name = \"Vikas Sharma\"\r\n        self.course = \"B.Tech\"\r\n\r\nclass Marks(Student):\r\n    def __init__(self):\r\n        super().__init__()  # Call the constructor of the base class 'Student'\r\n        self.per = 90  # Additional instance variable specific to the 'Marks' class\r\n\r\n    def display(self):\r\n        # Method to display instance variables of both 'Student' and 'Marks' classes\r\n        print(self.per)\r\n        print(\"------------------\")\r\n        print(self.rno)\r\n        print(self.name)\r\n        print(self.course)\r\n\r\n# Creating an instance of the 'Marks' class\r\nM1 = Marks()\r\n\r\n# Calling the 'display' method to print instance variables of both 'Student' and 'Marks' classes\r\nM1.display()<\/pre>\n<p>&lt;div class=&#8221;df-code-out&#8221;<strong>Output:<\/strong><br \/>\n90<br \/>\n&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;<br \/>\n100<br \/>\nVikas Sharma<br \/>\nB.Tech<\/p>\n<h4>Code Explanation:<\/h4>\n<ul>\n<li>Student class defines the init() constructor which initializes attributes rno, name, course<\/li>\n<li>Marks class inherits from Student<\/li>\n<li>Marks class init() calls super().init() before doing anything else<\/li>\n<li>super() calls the parent class (Student) init() method<\/li>\n<li>This ensures Student.init() executes first and creates rno, name, course attributes<\/li>\n<li>Marks.init() then initializes its own attribute per<\/li>\n<li>display() method prints per attribute initialized in Marks class<\/li>\n<li>It also prints rno, name, course inherited from parent Student class<\/li>\n<li>When M1 Marks object is created, it first calls Student.init() to initialize parent attributes due to super()<\/li>\n<li>Then Marks.init() sets its own per attribute<\/li>\n<li>So calling methods\/attributes on M1 gives access to both parent and child attributes<\/li>\n<li>Overall, super() ensures parent initialization happens before child initialization<\/li>\n<\/ul>\n<h3>Summary<\/h3>\n<p>In conclusion, the provided code underscores the integral role of constructors in the inheritance paradigm of Python. By inheriting attributes from the &#8216;Student&#8217; base class and extending functionality in the &#8216;Marks&#8217; derived class, developers can create efficient and modular code.<\/p>\n<p>The article elucidates the seamless integration of constructors through the &#8216;super()&#8217; function, emphasizing how it enables the derived class to leverage the constructor of the base class. As Python continues to be a prominent language for OOP, mastering the interplay between constructors and inheritance becomes imperative for crafting scalable and maintainable code.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In the realm of object-oriented programming, inheritance stands as a powerful mechanism, enabling the creation of new classes based on existing ones. Python, known for its simplicity and versatility, employs constructors to facilitate the&#46;&#46;&#46;<\/p>\n","protected":false},"author":581,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[46],"tags":[28856,10333,28626,28855],"class_list":["post-125742","post","type-post","status-publish","format-standard","hentry","category-python","tag-constructor-or-init-method-in-python-inheritance","tag-python","tag-python-practical","tag-role-of-constructor-in-python-inheritance"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.8 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Python Program on Constructor Role in Inheritance - DataFlair<\/title>\n<meta name=\"description\" content=\"This article delves into the provided code, shedding light on the role of constructors in inheritance. The provided code underscores the integral role of constructors in inheritance.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/data-flair.training\/blogs\/python-program-on-constructor-role-in-inheritance\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Python Program on Constructor Role in Inheritance - DataFlair\" \/>\n<meta property=\"og:description\" content=\"This article delves into the provided code, shedding light on the role of constructors in inheritance. The provided code underscores the integral role of constructors in inheritance.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/data-flair.training\/blogs\/python-program-on-constructor-role-in-inheritance\/\" \/>\n<meta property=\"og:site_name\" content=\"DataFlair\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/DataFlairWS\/\" \/>\n<meta property=\"article:published_time\" content=\"2023-11-09T11:44:51+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-02-28T06:45:15+00:00\" \/>\n<meta name=\"author\" content=\"DataFlair Team\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@DataFlairWS\" \/>\n<meta name=\"twitter:site\" content=\"@DataFlairWS\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"DataFlair Team\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"3 minutes\" \/>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Python Program on Constructor Role in Inheritance - DataFlair","description":"This article delves into the provided code, shedding light on the role of constructors in inheritance. The provided code underscores the integral role of constructors in inheritance.","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:\/\/data-flair.training\/blogs\/python-program-on-constructor-role-in-inheritance\/","og_locale":"en_US","og_type":"article","og_title":"Python Program on Constructor Role in Inheritance - DataFlair","og_description":"This article delves into the provided code, shedding light on the role of constructors in inheritance. The provided code underscores the integral role of constructors in inheritance.","og_url":"https:\/\/data-flair.training\/blogs\/python-program-on-constructor-role-in-inheritance\/","og_site_name":"DataFlair","article_publisher":"https:\/\/www.facebook.com\/DataFlairWS\/","article_published_time":"2023-11-09T11:44:51+00:00","article_modified_time":"2024-02-28T06:45:15+00:00","author":"DataFlair Team","twitter_card":"summary_large_image","twitter_creator":"@DataFlairWS","twitter_site":"@DataFlairWS","twitter_misc":{"Written by":"DataFlair Team","Est. reading time":"3 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/data-flair.training\/blogs\/python-program-on-constructor-role-in-inheritance\/#article","isPartOf":{"@id":"https:\/\/data-flair.training\/blogs\/python-program-on-constructor-role-in-inheritance\/"},"author":{"name":"DataFlair Team","@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/person\/c187795dc82ab948373cca526df7c445"},"headline":"Python Program on Constructor Role in Inheritance","datePublished":"2023-11-09T11:44:51+00:00","dateModified":"2024-02-28T06:45:15+00:00","mainEntityOfPage":{"@id":"https:\/\/data-flair.training\/blogs\/python-program-on-constructor-role-in-inheritance\/"},"wordCount":499,"commentCount":0,"publisher":{"@id":"https:\/\/data-flair.training\/blogs\/#organization"},"keywords":["constructor or init method in python inheritance","Python","python practical","role of constructor in python inheritance"],"articleSection":["Python Tutorials"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/data-flair.training\/blogs\/python-program-on-constructor-role-in-inheritance\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/data-flair.training\/blogs\/python-program-on-constructor-role-in-inheritance\/","url":"https:\/\/data-flair.training\/blogs\/python-program-on-constructor-role-in-inheritance\/","name":"Python Program on Constructor Role in Inheritance - DataFlair","isPartOf":{"@id":"https:\/\/data-flair.training\/blogs\/#website"},"datePublished":"2023-11-09T11:44:51+00:00","dateModified":"2024-02-28T06:45:15+00:00","description":"This article delves into the provided code, shedding light on the role of constructors in inheritance. The provided code underscores the integral role of constructors in inheritance.","breadcrumb":{"@id":"https:\/\/data-flair.training\/blogs\/python-program-on-constructor-role-in-inheritance\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/data-flair.training\/blogs\/python-program-on-constructor-role-in-inheritance\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/data-flair.training\/blogs\/python-program-on-constructor-role-in-inheritance\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Blog Home","item":"https:\/\/data-flair.training\/blogs\/"},{"@type":"ListItem","position":2,"name":"Python Tutorials","item":"https:\/\/data-flair.training\/blogs\/category\/python\/"},{"@type":"ListItem","position":3,"name":"Python Program on Constructor Role in Inheritance"}]},{"@type":"WebSite","@id":"https:\/\/data-flair.training\/blogs\/#website","url":"https:\/\/data-flair.training\/blogs\/","name":"DataFlair","description":"Learn Today. Lead Tomorrow.","publisher":{"@id":"https:\/\/data-flair.training\/blogs\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/data-flair.training\/blogs\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/data-flair.training\/blogs\/#organization","name":"DataFlair","url":"https:\/\/data-flair.training\/blogs\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/logo\/image\/","url":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2016\/07\/Data-Flair.png","contentUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2016\/07\/Data-Flair.png","width":106,"height":48,"caption":"DataFlair"},"image":{"@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/DataFlairWS\/","https:\/\/x.com\/DataFlairWS","https:\/\/www.linkedin.com\/company\/dataflair-web-services-pvt-ltd\/","https:\/\/www.youtube.com\/user\/DataFlairWS"]},{"@type":"Person","@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/person\/c187795dc82ab948373cca526df7c445","name":"DataFlair Team","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/2302ebc438084d2f1f993edc1996a0aae01332e81f3227cba8df0c48ec010ca4?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/2302ebc438084d2f1f993edc1996a0aae01332e81f3227cba8df0c48ec010ca4?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/2302ebc438084d2f1f993edc1996a0aae01332e81f3227cba8df0c48ec010ca4?s=96&d=mm&r=g","caption":"DataFlair Team"},"description":"DataFlair Team provides high-impact content on programming, Java, Python, C++, DSA, AI, ML, data Science, Android, Flutter, MERN, Web Development, and technology. We make complex concepts easy to grasp, helping learners of all levels succeed in their tech careers.","url":"https:\/\/data-flair.training\/blogs\/author\/dfteam6\/"}]}},"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/125742","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/users\/581"}],"replies":[{"embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/comments?post=125742"}],"version-history":[{"count":3,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/125742\/revisions"}],"predecessor-version":[{"id":134158,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/125742\/revisions\/134158"}],"wp:attachment":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/media?parent=125742"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/categories?post=125742"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/tags?post=125742"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}