{"id":8178,"date":"2023-08-20T08:00:16","date_gmt":"2023-08-20T02:30:16","guid":{"rendered":"https:\/\/tutorpython.com\/?p=8178"},"modified":"2023-10-19T15:15:44","modified_gmt":"2023-10-19T09:45:44","slug":"inheritance-in-python","status":"publish","type":"post","link":"https:\/\/tutorpython.com\/inheritance-in-python","title":{"rendered":"Inheritance in Python"},"content":{"rendered":"<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_80 counter-hierarchy ez-toc-counter ez-toc-transparent ez-toc-container-direction\">\n<div class=\"ez-toc-title-container\"><p class=\"ez-toc-title\" style=\"cursor:inherit\">In This Article<\/p>\n<\/div><nav><ul class='ez-toc-list ez-toc-list-level-1 ' ><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/tutorpython.com\/inheritance-in-python\/#Syntax_for_Inheritance_in_Python\" >Syntax for Inheritance in Python<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/tutorpython.com\/inheritance-in-python\/#Single_Inheritance_in_Python\" >Single Inheritance in Python<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/tutorpython.com\/inheritance-in-python\/#Multiple_Inheritance_in_Python\" >Multiple Inheritance in Python<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/tutorpython.com\/inheritance-in-python\/#Method_Overriding_in_Python_Inheritance\" >Method Overriding in Python Inheritance<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-5\" href=\"https:\/\/tutorpython.com\/inheritance-in-python\/#The_super_Method_in_Python_Inheritance\" >The super() Method in Python Inheritance<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-6\" href=\"https:\/\/tutorpython.com\/inheritance-in-python\/#Applications_of_Inheritance_in_Python\" >Applications of Inheritance in Python<\/a><\/li><\/ul><\/nav><\/div>\n<p><a href=\"https:\/\/tutorpython.com\/tutorial\/inheritance-in-python\">Inheritance<\/a> is a fundamental concept in <a href=\"https:\/\/tutorpython.com\/tutorial\/object-oriented-programming-in-python\" target=\"_blank\" rel=\"noopener\">object-oriented programming<\/a> (OOP) that allows classes to inherit attributes and methods from other classes. It provides a mechanism for creating new classes based on existing ones, thereby promoting code reuse and enhancing the organization and modularity of a program. Python, being an object-oriented programming language, fully supports inheritance and provides powerful tools to implement it effectively.<\/p>\n<p>Inheritance establishes a parent-child relationship between <a href=\"https:\/\/tutorpython.com\/tutorial\/object-oriented-programming-in-python\" target=\"_blank\" rel=\"noopener\">classes<\/a>, where the child class (also known as the derived class or subclass) inherits the properties of the parent class (also known as the base class or superclass).<\/p>\n<p>This means that the child class can access and utilize the attributes and methods defined in the parent class without having to redefine them. In addition, the child class can extend or modify the inherited behavior by adding new attributes or methods or overriding the existing ones.<\/p>\n<h2><span class=\"ez-toc-section\" id=\"Syntax_for_Inheritance_in_Python\"><\/span>Syntax for Inheritance in Python<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>To implement inheritance in Python, we use the syntax:<\/p>\n<pre><code class=\"language-python\">class ChildClass(ParentClass):\r\n    # Class body\r\n<\/code><\/pre>\n<p>Here, <code>ChildClass<\/code> is the name of the child&#8217;s class, and <code>ParentClass<\/code> is the name of the parent class. The child class can inherit from multiple parent classes, forming a hierarchy of classes.<\/p>\n<p>In Python, inheritance allows us to build complex class hierarchies and create specialized classes that inherit and extend the behavior of more general classes. It promotes code reusability, enhances the readability of the code, and simplifies maintenance and updates.<\/p>\n<h2><span class=\"ez-toc-section\" id=\"Single_Inheritance_in_Python\"><\/span>Single Inheritance in Python<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Single inheritance in Python refers to the process of creating a new class that inherits the attributes and methods of a single-parent class. It establishes a hierarchical relationship where the child class inherits all the characteristics of the parent class while being able to add its own unique attributes and methods.<\/p>\n<p>Example of single inheritance in Python-<\/p>\n<pre><code class=\"language-python\">class Animal:\r\n    def __init__(self, name):\r\n        self.name = name\r\n\r\n    def eat(self):\r\n        print(f\"{self.name} is eating.\")\r\n\r\nclass Dog(Animal):\r\n    def bark(self):\r\n        print(\"Woof!\")\r\n\r\ndog = Dog(\"Buddy\")\r\ndog.eat()  # Inherited from Animal class\r\ndog.bark()  # Defined in Dog class\r\n<\/code><\/pre>\n<p>Output:<\/p>\n<pre><code class=\"language-python\">Buddy is eating.\r\nWoof!\r\n<\/code><\/pre>\n<p>Here, the <code>Animal<\/code> class is the parent class, and the <code>Dog<\/code> class is the child class inheriting from <code>Animal<\/code>. The <code>Dog<\/code> class inherits the <code>eat()<\/code> method from the <code>Animal<\/code> class and defines its own method <code>bark()<\/code>. The <code>dog<\/code> <a href=\"https:\/\/tutorpython.com\/tutorial\/class-objects-in-python\" target=\"_blank\" rel=\"noopener\">object<\/a> can access both the inherited method <code>eat()<\/code> and the unique method <code>bark()<\/code>.<\/p>\n<h2><span class=\"ez-toc-section\" id=\"Multiple_Inheritance_in_Python\"><\/span>Multiple Inheritance in Python<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Multiple inheritance in Python refers to the ability of a class to inherit attributes and methods from multiple parent classes. It allows a child class to combine functionalities from multiple sources, incorporating the characteristics of multiple parent classes into a single derived class.<\/p>\n<p>Example of multiple inheritance in Python-<\/p>\n<pre><code class=\"language-python\">class A:\r\n    def method(self):\r\n        print(\"Method from class A\")\r\n\r\nclass B:\r\n    def method(self):\r\n        print(\"Method from class B\")\r\n\r\nclass C(A, B):\r\n    pass\r\n\r\nc = C()\r\nc.method()  # Method resolution order follows C -&gt; A -&gt; B\r\n<\/code><\/pre>\n<p>Output:<\/p>\n<pre><code class=\"language-python\">Method from class A\r\n<\/code><\/pre>\n<p>Here, we have three classes: <code>A<\/code>, <code>B<\/code>, and <code>C<\/code>. Class <code>C<\/code> inherits from both <code>A<\/code> and <code>B<\/code> using multiple inheritance. When the <code>method()<\/code> is called on the <code>c<\/code> object, Python looks for the <a href=\"https:\/\/tutorpython.com\/tutorial\/functions-in-python\" target=\"_blank\" rel=\"noopener\">method<\/a> in <code>C<\/code> first, then in <code>A<\/code>, and finally in <code>B<\/code>. Since <code>C<\/code> inherits from <code>A<\/code>, the method from <code>A<\/code> is executed.<\/p>\n<h2><span class=\"ez-toc-section\" id=\"Method_Overriding_in_Python_Inheritance\"><\/span>Method Overriding in Python Inheritance<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Method overriding is a fundamental aspect of inheritance in Python.<\/p>\n<p>It allows a child class to provide its own implementation of a method that is already defined in its parent class. By overriding a method, the child class can customize the behavior of the inherited method to suit its specific needs.<\/p>\n<p>To override a method in Python, the child class simply redefines the method with the same name as the method in the parent class. When an object of the child class calls the overridden method, the implementation in the child class is executed instead of the parent class.<\/p>\n<p>Example of method overriding in Python Inheritance-<\/p>\n<pre><code class=\"language-python\">class Animal:\r\n    def make_sound(self):\r\n        print(\"Generic animal sound.\")\r\n\r\nclass Dog(Animal):\r\n    def make_sound(self):\r\n        print(\"Woof!\")\r\n\r\nclass Cat(Animal):\r\n    def make_sound(self):\r\n        print(\"Meow!\")\r\n\r\n# Creating objects of the child classes\r\ndog = Dog()\r\ncat = Cat()\r\n\r\n# Calling the overridden method\r\ndog.make_sound()  # Output: Woof!\r\ncat.make_sound()  # Output: Meow!\r\n<\/code><\/pre>\n<p>Here, we have a parent class <code>Animal<\/code> with a method <code>make_sound()<\/code>. The child classes <code>Dog<\/code> and <code>Cat<\/code> override the <code>make_sound()<\/code> method with their own implementations.<\/p>\n<p>When the <code>make_sound()<\/code> method is called on a <code>dog<\/code> object, the overridden method in the <code>Dog<\/code> class is executed, resulting in the output &#8220;Woof!&#8221;.<\/p>\n<p>Similarly, when the method is called on a <code>cat<\/code> object, the overridden method in the <code>Cat<\/code> class is executed, producing the output &#8220;Meow!&#8221;.<\/p>\n<h2><span class=\"ez-toc-section\" id=\"The_super_Method_in_Python_Inheritance\"><\/span>The super() Method in Python Inheritance<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>In Python inheritance, the <code>super()<\/code> method plays a crucial role in facilitating the interaction between a child class and its parent class.<\/p>\n<p>It provides a way to access and invoke methods or constructors defined in the parent class, enabling the child class to extend or augment the behavior of the parent class.<\/p>\n<p>The <code>super()<\/code> method is typically used within the child class to call the parent class&#8217;s methods or constructors. By doing so, it allows the child class to utilize the existing functionality implemented in the parent class while adding its own modifications.<\/p>\n<p>Example:<\/p>\n<pre><code class=\"language-python\">class Parent:\r\n    def __init__(self):\r\n        self.parent_var = \"I am from the parent class.\"\r\n\r\n    def print_info(self):\r\n        print(\"This is the parent class.\")\r\n\r\nclass Child(Parent):\r\n    def __init__(self):\r\n        super().__init__()  # Calling the parent class's constructor\r\n        self.child_var = \"I am from the child class.\"\r\n\r\n    def print_info(self):\r\n        super().print_info()  # Calling the parent class's method\r\n        print(\"This is the child class.\")\r\n\r\n# Creating an object of the child class\r\nchild = Child()\r\n\r\n# Accessing the variables and invoking methods\r\nprint(child.parent_var)  # Output: I am from the parent class.\r\nprint(child.child_var)  # Output: I am from the child class.\r\nchild.print_info()  # Output: This is the parent class. This is the child class.\r\n<\/code><\/pre>\n<p>Here, the <code>Child<\/code> class inherits from the <code>Parent<\/code> class.<\/p>\n<p>The <code>Child<\/code> class&#8217;s constructor uses <code>super()<\/code> to call the parent class&#8217;s constructor, ensuring that the initialization steps in the parent class are executed. This allows the child class to inherit and utilize the <code>parent_var<\/code> attribute.<\/p>\n<p>Similarly, the <code>Child<\/code> class overrides the <code>print_info()<\/code> method, but within the overridden method, it calls <code>super().print_info()<\/code> to invoke the parent class&#8217;s <code>print_info()<\/code> method.<\/p>\n<p>This way, the child class can add its own behavior while maintaining the functionality provided by the parent class.<\/p>\n<p>The <code>super()<\/code> method is a powerful tool when working with inheritance, as it ensures proper coordination and cooperation between the parent and child classes. It allows for code reuse, modularity, and flexibility in implementing complex class hierarchies.<\/p>\n<h2><span class=\"ez-toc-section\" id=\"Applications_of_Inheritance_in_Python\"><\/span>Applications of Inheritance in Python<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Here are some common applications of inheritance:<\/p>\n<ul>\n<li><strong>Code Reusability:<\/strong> Inheritance allows us to reuse code from existing classes. By inheriting from a base class, we can acquire its attributes and methods, reducing code duplication. This promotes modular and maintainable code, as changes made in the base class automatically propagate to the derived classes.<\/li>\n<li><strong>Specialization and Generalization:<\/strong> Inheritance facilitates the creation of specialized classes that inherit from more general classes. This enables us to model real-world relationships and hierarchies. For example, a &#8220;Vehicle&#8221; base class can have derived classes like &#8220;Car,&#8221; &#8220;Motorcycle,&#8221; and &#8220;Truck,&#8221; each with its own specific attributes and behaviors.<\/li>\n<li><strong>Method Overriding:<\/strong> Inheritance allows child classes to override methods inherited from parent classes. This is useful when we want to modify the behavior of a method in a child class without changing the parent class implementation. It promotes customization and fine-tuning of functionalities.<\/li>\n<li><strong>Polymorphism:<\/strong> Inheritance, along with method overriding, enables polymorphism. Polymorphism allows objects of different classes to be treated as objects of a common base class. This provides flexibility and allows for more generic code that can work with different types of objects.<\/li>\n<li><strong>Interface Definition:<\/strong> Inheritance can be used to define interfaces or <a href=\"https:\/\/tutorpython.com\/tutorial\/abstraction-in-python\" target=\"_blank\" rel=\"noopener\">abstract classes<\/a> that establish a common set of methods that derived classes must implement. This helps in enforcing a certain structure and ensures that derived classes adhere to a specific contract.<\/li>\n<li><strong>Multiple Inheritance:<\/strong> Python supports multiple inheritance, where a class can inherit from multiple parent classes. This feature allows us to combine functionalities from different classes, promoting code reuse and flexibility. However, it requires careful management to avoid conflicts or ambiguity.<\/li>\n<li><strong>Frameworks and Libraries:<\/strong> Inheritance is extensively used in frameworks and libraries to provide a foundation for developers to build upon. Frameworks often define base classes with default behaviors, and developers extend those classes to customize and add their specific functionalities.<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>Inheritance is a fundamental concept in object-oriented programming (OOP) that allows classes&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[28],"tags":[],"class_list":["post-8178","post","type-post","status-publish","format-standard","hentry","category-tutorial"],"acf":[],"_links":{"self":[{"href":"https:\/\/tutorpython.com\/wp-json\/wp\/v2\/posts\/8178","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/tutorpython.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/tutorpython.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/tutorpython.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/tutorpython.com\/wp-json\/wp\/v2\/comments?post=8178"}],"version-history":[{"count":6,"href":"https:\/\/tutorpython.com\/wp-json\/wp\/v2\/posts\/8178\/revisions"}],"predecessor-version":[{"id":8863,"href":"https:\/\/tutorpython.com\/wp-json\/wp\/v2\/posts\/8178\/revisions\/8863"}],"wp:attachment":[{"href":"https:\/\/tutorpython.com\/wp-json\/wp\/v2\/media?parent=8178"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/tutorpython.com\/wp-json\/wp\/v2\/categories?post=8178"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/tutorpython.com\/wp-json\/wp\/v2\/tags?post=8178"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}