{"id":986,"date":"2023-02-09T17:41:00","date_gmt":"2023-02-09T12:11:00","guid":{"rendered":"https:\/\/geekpython.in\/?p=986"},"modified":"2023-08-15T14:26:27","modified_gmt":"2023-08-15T08:56:27","slug":"class-inheritance-in-python","status":"publish","type":"post","link":"https:\/\/geekpython.in\/class-inheritance-in-python","title":{"rendered":"Inheritance In Python &#8211; Single, Multiple, Multi-level Inheritance And More"},"content":{"rendered":"\n<p>We all have ever encountered the term&nbsp;<strong>OOP<\/strong>s (Object-Oriented Programming) in programming. OOP is an integral part of any programming language that helps carry out complex concepts. It provides a way to structure and organize the code in such a manner that makes it easier to maintain and scale the code.<\/p>\n\n\n\n<p>Classes are an essential concept in OOP or we can say that classes are the building blocks of Object-Oriented Programming which is a programming model based on objects and classes.<\/p>\n\n\n\n<p>Object-Oriented Programming has four important concepts and one of them is&nbsp;<strong>inheritance<\/strong>.<\/p>\n\n\n\n<h1 class=\"wp-block-heading\" id=\"heading-what-is-inheritance\"><a href=\"https:\/\/geekpython.in\/class-inheritance-in-python#heading-what-is-inheritance\"><\/a>What is Inheritance?<\/h1>\n\n\n\n<p><strong>Inheritance<\/strong>&nbsp;can be defined as the mechanism that permits the newly created classes to inherit the methods and attributes of the existing class or parent class. The classes that inherit the methods and attributes from the parent class are called&nbsp;<strong>subclass<\/strong>&nbsp;and the existing class is called a&nbsp;<strong>superclass<\/strong>.<\/p>\n\n\n\n<p>As soon as a subclass inherits the superclass, it gains all of the methods and attributes of the superclass. This allows us to reuse the code as well as we can extend or modify the behavior of the superclass.<\/p>\n\n\n\n<p>For example-<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:python decode:true \">class Hero:\n    def power(self):\n        print(\"Rich\")\n\nclass Ironman(Hero):\n    def speciality(self):\n        print(\"Genius\", \"Millionaire\", \"Playboy\", \"Philanthropist\")\n\nobj = Ironman()\nobj.power()\nobj.speciality()<\/pre><\/div>\n\n\n\n<p>The class&nbsp;<code>Hero<\/code>&nbsp;has a method&nbsp;<code>power<\/code>&nbsp;that returns a specific power a hero has. Then we created a subclass&nbsp;<code>Ironman<\/code>&nbsp;that inherits the superclass&nbsp;<code>Hero<\/code>&nbsp;and the subclass&nbsp;<code>Ironman<\/code>&nbsp;has its own method&nbsp;<code>speciality<\/code>&nbsp;that specifies the specialties of Ironman.<\/p>\n\n\n\n<p>The subclass&nbsp;<code>Ironman<\/code>&nbsp;now has access to all the attributes and methods of the superclass&nbsp;<code>Hero<\/code>&nbsp;as well.<\/p>\n\n\n\n<p>We accessed the method&nbsp;<code>power<\/code>&nbsp;using the class&nbsp;<code>Ironman<\/code>&nbsp;which inherited the method&nbsp;<code>power<\/code>&nbsp;from the class&nbsp;<code>Hero<\/code>.<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:python decode:true \">Rich\nGenius Millionaire Playboy Philanthropist<\/pre><\/div>\n\n\n\n<p>Inheritance helps us reduce the effort that we put into writing the same logic again and again and makes it easy to maintain and update the code.<\/p>\n\n\n\n<p>Different types of inheritance:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Single Inheritance<\/strong><\/li>\n\n\n\n<li><strong>Multiple Inheritance<\/strong><\/li>\n\n\n\n<li><strong>Multilevel Inheritance<\/strong><\/li>\n\n\n\n<li><strong>Hierarchical Inheritance<\/strong><\/li>\n\n\n\n<li><strong>Hybrid Inheritance<\/strong><\/li>\n<\/ul>\n\n\n\n<h1 class=\"wp-block-heading\" id=\"heading-single-inheritance\"><a href=\"https:\/\/geekpython.in\/class-inheritance-in-python#heading-single-inheritance\"><\/a>Single Inheritance<\/h1>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"546\" src=\"https:\/\/geekpython.in\/wp-content\/uploads\/2023\/08\/single-1024x546.png\" alt=\"Single Inheritance\" class=\"wp-image-989\" srcset=\"https:\/\/geekpython.in\/wp-content\/uploads\/2023\/08\/single-1024x546.png 1024w, https:\/\/geekpython.in\/wp-content\/uploads\/2023\/08\/single-300x160.png 300w, https:\/\/geekpython.in\/wp-content\/uploads\/2023\/08\/single-768x410.png 768w, https:\/\/geekpython.in\/wp-content\/uploads\/2023\/08\/single.png 1500w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p><strong>Single inheritance<\/strong>&nbsp;can be defined as an inheritance where a&nbsp;<strong>subclass<\/strong>&nbsp;or&nbsp;<strong>derived class<\/strong>&nbsp;inherits from a single&nbsp;<strong>superclass<\/strong>&nbsp;or&nbsp;<strong>parent class<\/strong>.<\/p>\n\n\n\n<p>In simple words, the&nbsp;<strong>derived class or subclass has only one direct parent class<\/strong>. Here is an example of single inheritance.<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:python decode:true \"># Parent or Base Class\nclass Calculate:\n    def sum(self, a, b):\n        print(a + b)\n\n# Subclass or Derived Class\nclass Number(Calculate):\n    pass\n\nobj = Number()\nobj.sum(8, 90)\n\n----------\n98<\/pre><\/div>\n\n\n\n<p>The above code is no different from the code that we discussed earlier where we saw the first glimpse of class inheritance in Python.<\/p>\n\n\n\n<p>The subclass&nbsp;<code>Number<\/code>&nbsp;inherited the superclass&nbsp;<code>Calculate<\/code>&nbsp;and now the methods inside it can now be accessed by the subclass&nbsp;<code>Number<\/code>. We created the instance for the subclass&nbsp;<code>Number<\/code>&nbsp;and then accessed the&nbsp;<code>sum<\/code>&nbsp;function from the superclass or parent class&nbsp;<code>Calculate<\/code>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"heading-using-constructor\"><a href=\"https:\/\/geekpython.in\/class-inheritance-in-python#heading-using-constructor\"><\/a>Using constructor<\/h2>\n\n\n\n<p>Until now, we saw a simple demonstration of the class inheritance in which a subclass gains the methods and attributes of the superclass by just passing it as an argument.<\/p>\n\n\n\n<p>Now we&#8217;ll see how to perform inheritance when a class has a constructor function.<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:python decode:true \"># Base class\nclass Hero:\n    # Constructor function\n    def __init__(self, reel_name, hero_name):\n        self.reel_name = reel_name\n        self.hero_name = hero_name\n\n    def show(self):\n        print(self.reel_name)\n        print(self.hero_name)\n\n# Derived class\nclass Name(Hero):\n    def __init__(self, real_name, reel_name, hero_name):\n        self.real_name = real_name\n        # Calling the __init__ of the parent class\n        Hero.__init__(self, reel_name, hero_name)\n\n    def display(self):\n        print(self.real_name)\n        Hero.show(self)\n\nobj = Name(\"RDJ\", \"Tony Stark\", \"IronMan\")\nobj.display()\nprint(\"-\"*20)\nobj.show()<\/pre><\/div>\n\n\n\n<p>In the above code, we created the base class&nbsp;<code>Hero<\/code>&nbsp;and inside it, we defined the constructor function that takes two arguments&nbsp;<code>reel_name<\/code>&nbsp;and&nbsp;<code>hero_name<\/code>&nbsp;and then we defined the function&nbsp;<code>show<\/code>&nbsp;that prints the&nbsp;<code>reel_name<\/code>&nbsp;and&nbsp;<code>hero_name<\/code>.<\/p>\n\n\n\n<p>Then we created a subclass&nbsp;<code>Name<\/code>&nbsp;that inherits the superclass&nbsp;<code>Hero<\/code>&nbsp;and then we created a constructor function that takes three arguments&nbsp;<code>real_name<\/code>,&nbsp;<code>reel_name<\/code>, and&nbsp;<code>hero_name<\/code>.<\/p>\n\n\n\n<p>In order to access the objects of the parent class&nbsp;<code>Hero<\/code>, we invoked&nbsp;<code>__init__<\/code>&nbsp;of the parent class&nbsp;<code>Hero<\/code>&nbsp;and passed the required arguments.<\/p>\n\n\n\n<p>Next, we created a function&nbsp;<code>display<\/code>&nbsp;that prints the&nbsp;<code>real_name<\/code>&nbsp;and calls the function&nbsp;<code>show<\/code>&nbsp;from the class&nbsp;<code>Hero<\/code>.<\/p>\n\n\n\n<p>Then we created&nbsp;<code>obj<\/code>&nbsp;an instance of the class&nbsp;<code>Name<\/code>&nbsp;and called the function&nbsp;<code>display<\/code>&nbsp;and&nbsp;<code>show<\/code>.<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:python decode:true \">RDJ\nTony Stark\nIronMan\n--------------------\nTony Stark\nIronMan<\/pre><\/div>\n\n\n\n<p><strong>What if we didn&#8217;t call the<\/strong>&nbsp;<code>__init__<\/code>&nbsp;<strong>of the parent class?<\/strong><\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:python decode:true \">class Cal:\n    def __init__(self, a, b):\n        self.a = a\n        self.b = b\n\n    def sq(self):\n        print(self.a ** self.b)\n\nclass X(Cal):\n    def __init__(self, c):\n        self.c = c\n\nobj = X(\"Hello\")\nobj.sq()<\/pre><\/div>\n\n\n\n<p>We have done everything similar to what we did earlier but this time we didn&#8217;t call the&nbsp;<code>__init__<\/code>&nbsp;of the parent class&nbsp;<code>Cal<\/code>&nbsp;and tried to call the function&nbsp;<code>sq<\/code>&nbsp;from it.<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:python decode:true \">Traceback (most recent call last):\n  ...\n    print(self.a ** self.b)\nAttributeError: 'X' object has no attribute 'a'<\/pre><\/div>\n\n\n\n<p>We encountered an error and this happened because the objects of the parent class&nbsp;<code>Cal<\/code>&nbsp;were not available for the subclass or child class&nbsp;<code>X<\/code>.<\/p>\n\n\n\n<h1 class=\"wp-block-heading\" id=\"heading-multiple-inheritance\"><a href=\"https:\/\/geekpython.in\/class-inheritance-in-python#heading-multiple-inheritance\"><\/a>Multiple Inheritance<\/h1>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"546\" src=\"https:\/\/geekpython.in\/wp-content\/uploads\/2023\/08\/multiple-1024x546.png\" alt=\"Multiple Inheritance\" class=\"wp-image-990\" srcset=\"https:\/\/geekpython.in\/wp-content\/uploads\/2023\/08\/multiple-1024x546.png 1024w, https:\/\/geekpython.in\/wp-content\/uploads\/2023\/08\/multiple-300x160.png 300w, https:\/\/geekpython.in\/wp-content\/uploads\/2023\/08\/multiple-768x410.png 768w, https:\/\/geekpython.in\/wp-content\/uploads\/2023\/08\/multiple.png 1500w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>Python supports&nbsp;<strong>multiple-class inheritance<\/strong>&nbsp;and can be defined as an inheritance where a subclass or child class inherits from more than one superclass.<\/p>\n\n\n\n<p>In short,&nbsp;<strong>a subclass has more than one direct parent class or superclass<\/strong>. Let&#8217;s understand the concept of multiple-class inheritance with an example.<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:python decode:true \"># First Parent Class\nclass First:\n    def __init__(self):\n        self.greet = \"Hello\"\n\n# Second Parent Class\nclass Second:\n    def __init__(self):\n        self.name = \"Geeks\"\n\n# Child or Derived Class\nclass Child(First, Second):\n    def __init__(self):\n        First.__init__(self)\n        Second.__init__(self)\n\n    def combine(self):\n        print(self.greet, self.name)\n        print(\"Welcome to GeekPython.\")\n\nobj = Child()\nobj.combine()<\/pre><\/div>\n\n\n\n<p>In the above code, subclass&nbsp;<code>Child<\/code>&nbsp;inherited two superclasses named&nbsp;<code>First<\/code>&nbsp;and&nbsp;<code>Second<\/code>&nbsp;that will help gain the attributes of both superclasses inside the subclass&nbsp;<code>Child<\/code>.<\/p>\n\n\n\n<p>We then created a function&nbsp;<code>combine<\/code>&nbsp;that prints the values of&nbsp;<code>self.greet<\/code>&nbsp;and&nbsp;<code>self.name<\/code>&nbsp;and a string. We created&nbsp;<code>obj<\/code>&nbsp;which is an instance of the class and then called the&nbsp;<code>combine<\/code>&nbsp;function.<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:python decode:true \">Hello Geeks\nWelcome to GeekPython.<\/pre><\/div>\n\n\n\n<p>Note: There is a risk of using multiple inheritances because it can lead to uncertainty in the code and also cause a &#8220;<strong>Diamond Problem<\/strong>&#8221; where a subclass inherits certain conflicting methods from multiple parent classes.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"heading-diamond-problem\"><a href=\"https:\/\/geekpython.in\/class-inheritance-in-python#heading-diamond-problem\"><\/a>Diamond Problem<\/h2>\n\n\n\n<p>The &#8220;<strong>Diamond Problem<\/strong>&#8221; often occurs primarily in the case of multiple inheritance where a subclass inherits the conflicting methods from the multiple superclasses that eventually create ambiguity in the code.<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:python decode:true \"># Base Class\nclass Hello:\n    def func(self):\n        print(\"Hello\")\n\n# Derived Class\nclass Geek(Hello):\n    def func(self):\n        print(\"Geeks\")\n\n# Derived Class\nclass Python(Hello):\n    def func(self):\n        print(\"GeekPython\")\n\n# Derived Class\nclass GeekPython(Geek, Python):\n    pass\n\nobj = GeekPython()\nobj.func()<\/pre><\/div>\n\n\n\n<p>In the above code, subclasses&nbsp;<code>Geek<\/code>&nbsp;and&nbsp;<code>Python<\/code>&nbsp;inherit from the superclass&nbsp;<code>Hero<\/code>&nbsp;and then another subclass&nbsp;<code>GeekPython<\/code>&nbsp;inherits from the subclasses&nbsp;<code>Geek<\/code>&nbsp;and&nbsp;<code>Python<\/code>. They all have a function with the same name&nbsp;<code>func<\/code>.<\/p>\n\n\n\n<p>Now when we call the&nbsp;<code>func<\/code>, the order of the parent class decides from which class&nbsp;<code>GeekPython<\/code>&nbsp;inherits from. So, in this case, the function&nbsp;<code>func<\/code>&nbsp;inside the class&nbsp;<code>Geek<\/code>&nbsp;will get executed.<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:python decode:true \">Geeks<\/pre><\/div>\n\n\n\n<p><strong>If we change the order of the class, then we&#8217;ll get the following output<\/strong>.<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:python decode:true \"># Derived Class\nclass GeekPython(Python, Geek):\n    pass\n\n----------\nGeekPython<\/pre><\/div>\n\n\n\n<h1 class=\"wp-block-heading\" id=\"heading-multi-level-inheritance\"><a href=\"https:\/\/geekpython.in\/class-inheritance-in-python#heading-multi-level-inheritance\"><\/a>Multi-level Inheritance<\/h1>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"546\" src=\"https:\/\/geekpython.in\/wp-content\/uploads\/2023\/08\/multilevel-1024x546.png\" alt=\"Multilevel Inheritance\" class=\"wp-image-991\" srcset=\"https:\/\/geekpython.in\/wp-content\/uploads\/2023\/08\/multilevel-1024x546.png 1024w, https:\/\/geekpython.in\/wp-content\/uploads\/2023\/08\/multilevel-300x160.png 300w, https:\/\/geekpython.in\/wp-content\/uploads\/2023\/08\/multilevel-768x410.png 768w, https:\/\/geekpython.in\/wp-content\/uploads\/2023\/08\/multilevel.png 1500w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p><strong>Multi-level inheritance<\/strong>&nbsp;can be defined as where a&nbsp;<strong>subclass<\/strong>&nbsp;inherits from the&nbsp;<strong>single subclass<\/strong>&nbsp;and then&nbsp;<strong>another subclass<\/strong>&nbsp;inherits from the&nbsp;<strong>first subclass<\/strong>. By this, the second subclass can access all the attributes and methods from both the first subclass and the superclass.<\/p>\n\n\n\n<p>Let&#8217;s understand with an example showing multi-level inheritance.<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:python decode:true \"># Base Class\nclass GrandPa:\n    def __init__(self):\n        self.age = 100\n\n# Derived CLass\nclass Parent(GrandPa):\n    def __init__(self):\n        self.name = \"Geek\"\n        GrandPa.__init__(self)\n\n# Derived Class\nclass GrandChild(Parent):\n    def __init__(self):\n        self.hobby = \"Gaming\"\n        Parent.__init__(self)\n\n    def display(self):\n        print(\"Grandpa:\", self.age)\n        print(\"Parent:\", self.name)\n        print(\"Grandchild:\", self.hobby)\n\nobj = GrandChild()\nobj.display()<\/pre><\/div>\n\n\n\n<p>In the above code, the subclass&nbsp;<code>Parent<\/code>&nbsp;inherits from the superclass&nbsp;<code>GrandPa<\/code>&nbsp;and now the first subclass&nbsp;<code>Parent<\/code>&nbsp;has access to the methods of the superclass&nbsp;<code>GrandPa<\/code>.<\/p>\n\n\n\n<p>Then another subclass&nbsp;<code>GrandChild<\/code>&nbsp;inherited from the first subclass&nbsp;<code>Parent<\/code>&nbsp;and it has access to the methods that both the first subclass and superclass have.<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:python decode:true \">Grandpa: 100\nParent: Geek\nGrandchild: Gaming<\/pre><\/div>\n\n\n\n<h1 class=\"wp-block-heading\" id=\"heading-hierarchical-inheritance\"><a href=\"https:\/\/geekpython.in\/class-inheritance-in-python#heading-hierarchical-inheritance\"><\/a>Hierarchical Inheritance<\/h1>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"546\" src=\"https:\/\/geekpython.in\/wp-content\/uploads\/2023\/08\/hierarchical-1024x546.png\" alt=\"Hierarchical Inheritance\" class=\"wp-image-992\" srcset=\"https:\/\/geekpython.in\/wp-content\/uploads\/2023\/08\/hierarchical-1024x546.png 1024w, https:\/\/geekpython.in\/wp-content\/uploads\/2023\/08\/hierarchical-300x160.png 300w, https:\/\/geekpython.in\/wp-content\/uploads\/2023\/08\/hierarchical-768x410.png 768w, https:\/\/geekpython.in\/wp-content\/uploads\/2023\/08\/hierarchical.png 1500w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p><strong>Hierarchical inheritance<\/strong>&nbsp;can be defined as when&nbsp;<strong>more than one derived class inherits from a single base class<\/strong>. Let&#8217;s understand with an example.<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:python decode:true \"># Base Class\nclass Calculate:\n    def __init__(self, a, b):\n        self.a = a\n        self.b = b\n\n    def division(self):\n        print(self.a \/ self.b)\n\n# Derived Class\nclass Add(Calculate):\n    def __init__(self, a, b):\n        Calculate.__init__(self, a, b)\n\n    def add(self):\n        print(\"Addition:\", self.a + self.b)\n        Add.division(self)\n\n# Derived Class\nclass Subtract(Calculate):\n    def __init__(self, a, b):\n        Calculate.__init__(self, a, b)\n\n    def subtract(self):\n        print(\"Subtraction:\", self.a - self.b)\n        Subtract.division(self)\n\nobj1 = Add(34, 98)\nobj2 = Subtract(45, 67)\nobj1.add()\nobj2.subtract()<\/pre><\/div>\n\n\n\n<p>In the above code, both derived class&nbsp;<code>Add<\/code>&nbsp;and&nbsp;<code>Subtract<\/code>&nbsp;inherited from the superclass or base class&nbsp;<code>Calculate<\/code>.<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:python decode:true \">Addition: 132\n0.3469387755102041\nSubtraction: -22\n0.6716417910447762<\/pre><\/div>\n\n\n\n<h1 class=\"wp-block-heading\" id=\"heading-hybrid-inheritance\"><a href=\"https:\/\/geekpython.in\/class-inheritance-in-python#heading-hybrid-inheritance\"><\/a>Hybrid Inheritance<\/h1>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"546\" src=\"https:\/\/geekpython.in\/wp-content\/uploads\/2023\/08\/hybrid-1024x546.png\" alt=\"Hybrid Inheritance\" class=\"wp-image-993\" srcset=\"https:\/\/geekpython.in\/wp-content\/uploads\/2023\/08\/hybrid-1024x546.png 1024w, https:\/\/geekpython.in\/wp-content\/uploads\/2023\/08\/hybrid-300x160.png 300w, https:\/\/geekpython.in\/wp-content\/uploads\/2023\/08\/hybrid-768x410.png 768w, https:\/\/geekpython.in\/wp-content\/uploads\/2023\/08\/hybrid.png 1500w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>This type of inheritance is a blend of different inheritance which means it has a combination of two different types of inheritance like multiple or multi-level inheritances or multiple or single inheritances.<\/p>\n\n\n\n<p>Here is an example showing the demonstration of&nbsp;<strong>hybrid inheritance<\/strong>.<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:python decode:true \"># Base Class\nclass Role:\n    def role(self):\n        print(\"Protagonist\")\n\n# Derived Class\nclass Luffy(Role):\n    def power(self):\n        print(\"Gum-Gum devil fruit\")\n\n# Derived Class\nclass Naruto(Role):\n    def power1(self):\n        print(\"Nine-tail fox\")\n\n# Derived Class\nclass Anime(Luffy, Naruto):\n    def anime(self):\n        print(\"They are anime characters.\")\n\nobj = Anime()\nobj.role()\nobj.power()\nprint(\"-\"*20)\nobj.role()\nobj.power1()\nprint(\"-\"*20)\nobj.anime()<\/pre><\/div>\n\n\n\n<p>In the above code, the subclasses&nbsp;<code>Luffy<\/code>&nbsp;and&nbsp;<code>Naruto<\/code>&nbsp;inherit from the superclass&nbsp;<code>Role<\/code>&nbsp;and then the subclass Anime inherits from the subclasses&nbsp;<code>Luffy<\/code>&nbsp;and&nbsp;<code>Naruto<\/code>.<\/p>\n\n\n\n<p>The first scenario where&nbsp;<code>Luffy<\/code>&nbsp;and&nbsp;<code>Naruto<\/code>&nbsp;inherit from the superclass&nbsp;<code>Role<\/code>&nbsp;shows the&nbsp;<strong>single inheritance<\/strong>&nbsp;and the second scenario where&nbsp;<code>Anime<\/code>&nbsp;inherits from the subclasses&nbsp;<code>Luffy<\/code>&nbsp;and&nbsp;<code>Naruto<\/code>&nbsp;shows the multiple inheritances.<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:python decode:true \">Protagonist\nGum-Gum devil fruit\n--------------------\nProtagonist\nNine-tail fox\n--------------------\nThey are anime characters.<\/pre><\/div>\n\n\n\n<h1 class=\"wp-block-heading\" id=\"heading-conclusion\"><a href=\"https:\/\/geekpython.in\/class-inheritance-in-python#heading-conclusion\"><\/a>Conclusion<\/h1>\n\n\n\n<p><strong>Inheritance<\/strong>&nbsp;is one of the pillars of&nbsp;<strong>Object-Oriented Programming<\/strong>&nbsp;(OOP) that allows a new class to inherit the methods and attributes of an existing class or parent class.<\/p>\n\n\n\n<p>Python is one of the programming languages that support multiple inheritance of the class but it could lead to uncertainty in the code.<\/p>\n\n\n\n<p>Let&#8217;s review what we&#8217;ve learned:<\/p>\n\n\n\n<p>We saw the concepts of different types of inheritance in Python:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Single Inheritance<\/strong><\/li>\n\n\n\n<li><strong>Multiple Inheritance<\/strong><\/li>\n\n\n\n<li><strong>Multi-level Inheritance<\/strong><\/li>\n\n\n\n<li><strong>Hierarchical Inheritance<\/strong><\/li>\n\n\n\n<li><strong>Hybrid Inheritance<\/strong><\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p>\ud83c\udfc6<strong>Other articles you might like if you like this article<\/strong><\/p>\n\n\n\n<p>\u2705<a target=\"_blank\" href=\"https:\/\/geekpython.in\/access-modifiers-in-python\" rel=\"noreferrer noopener\">Public, Protected and Private access modifiers in Python<\/a>.<\/p>\n\n\n\n<p>\u2705<a target=\"_blank\" href=\"https:\/\/geekpython.in\/build-api-using-fastapi\" rel=\"noreferrer noopener\">Build your own API in Python using FastAPI in a few steps<\/a>.<\/p>\n\n\n\n<p>\u2705<a target=\"_blank\" href=\"https:\/\/geekpython.in\/f-string-in-python\" rel=\"noreferrer noopener\">A modern way to perform string interpolation in Python<\/a>.<\/p>\n\n\n\n<p>\u2705<a target=\"_blank\" href=\"https:\/\/geekpython.in\/argparse-in-python\" rel=\"noreferrer noopener\">Build your own CLI in Python using the argparse module<\/a>.<\/p>\n\n\n\n<p>\u2705<a target=\"_blank\" href=\"https:\/\/geekpython.in\/ways-to-remove-whitespaces-from-the-string-in-python-with-examples-beginners-guide\" rel=\"noreferrer noopener\">Different ways to remove whitespaces from the string in Python<\/a>.<\/p>\n\n\n\n<p>\u2705<a target=\"_blank\" href=\"https:\/\/geekpython.in\/python-bitwise-operators-with-examples-explained-in-detail\" rel=\"noreferrer noopener\">Bitwise operators explained with examples in Python<\/a>.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p><strong>That&#8217;s all for now<\/strong><\/p>\n\n\n\n<p><strong>Keep Coding\u270c\u270c<\/strong><\/p>\n","protected":false},"excerpt":{"rendered":"<p>We all have ever encountered the term&nbsp;OOPs (Object-Oriented Programming) in programming. OOP is an integral part of any programming language that helps carry out complex concepts. It provides a way to structure and organize the code in such a manner that makes it easier to maintain and scale the code. Classes are an essential concept [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":988,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"ocean_post_layout":"","ocean_both_sidebars_style":"","ocean_both_sidebars_content_width":0,"ocean_both_sidebars_sidebars_width":0,"ocean_sidebar":"0","ocean_second_sidebar":"0","ocean_disable_margins":"enable","ocean_add_body_class":"","ocean_shortcode_before_top_bar":"","ocean_shortcode_after_top_bar":"","ocean_shortcode_before_header":"","ocean_shortcode_after_header":"","ocean_has_shortcode":"","ocean_shortcode_after_title":"","ocean_shortcode_before_footer_widgets":"","ocean_shortcode_after_footer_widgets":"","ocean_shortcode_before_footer_bottom":"","ocean_shortcode_after_footer_bottom":"","ocean_display_top_bar":"default","ocean_display_header":"default","ocean_header_style":"","ocean_center_header_left_menu":"0","ocean_custom_header_template":"0","ocean_custom_logo":0,"ocean_custom_retina_logo":0,"ocean_custom_logo_max_width":0,"ocean_custom_logo_tablet_max_width":0,"ocean_custom_logo_mobile_max_width":0,"ocean_custom_logo_max_height":0,"ocean_custom_logo_tablet_max_height":0,"ocean_custom_logo_mobile_max_height":0,"ocean_header_custom_menu":"0","ocean_menu_typo_font_family":"0","ocean_menu_typo_font_subset":"","ocean_menu_typo_font_size":0,"ocean_menu_typo_font_size_tablet":0,"ocean_menu_typo_font_size_mobile":0,"ocean_menu_typo_font_size_unit":"px","ocean_menu_typo_font_weight":"","ocean_menu_typo_font_weight_tablet":"","ocean_menu_typo_font_weight_mobile":"","ocean_menu_typo_transform":"","ocean_menu_typo_transform_tablet":"","ocean_menu_typo_transform_mobile":"","ocean_menu_typo_line_height":0,"ocean_menu_typo_line_height_tablet":0,"ocean_menu_typo_line_height_mobile":0,"ocean_menu_typo_line_height_unit":"","ocean_menu_typo_spacing":0,"ocean_menu_typo_spacing_tablet":0,"ocean_menu_typo_spacing_mobile":0,"ocean_menu_typo_spacing_unit":"","ocean_menu_link_color":"","ocean_menu_link_color_hover":"","ocean_menu_link_color_active":"","ocean_menu_link_background":"","ocean_menu_link_hover_background":"","ocean_menu_link_active_background":"","ocean_menu_social_links_bg":"","ocean_menu_social_hover_links_bg":"","ocean_menu_social_links_color":"","ocean_menu_social_hover_links_color":"","ocean_disable_title":"default","ocean_disable_heading":"default","ocean_post_title":"","ocean_post_subheading":"","ocean_post_title_style":"","ocean_post_title_background_color":"","ocean_post_title_background":0,"ocean_post_title_bg_image_position":"","ocean_post_title_bg_image_attachment":"","ocean_post_title_bg_image_repeat":"","ocean_post_title_bg_image_size":"","ocean_post_title_height":0,"ocean_post_title_bg_overlay":0.5,"ocean_post_title_bg_overlay_color":"","ocean_disable_breadcrumbs":"default","ocean_breadcrumbs_color":"","ocean_breadcrumbs_separator_color":"","ocean_breadcrumbs_links_color":"","ocean_breadcrumbs_links_hover_color":"","ocean_display_footer_widgets":"default","ocean_display_footer_bottom":"default","ocean_custom_footer_template":"0","ocean_post_oembed":"","ocean_post_self_hosted_media":"","ocean_post_video_embed":"","ocean_link_format":"","ocean_link_format_target":"self","ocean_quote_format":"","ocean_quote_format_link":"post","ocean_gallery_link_images":"off","ocean_gallery_id":[],"footnotes":""},"categories":[2,24],"tags":[26,12,31],"class_list":["post-986","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-python","category-object-oriented","tag-oop","tag-python","tag-python3","entry","has-media"],"_links":{"self":[{"href":"https:\/\/geekpython.in\/wp-json\/wp\/v2\/posts\/986","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/geekpython.in\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/geekpython.in\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/geekpython.in\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/geekpython.in\/wp-json\/wp\/v2\/comments?post=986"}],"version-history":[{"count":3,"href":"https:\/\/geekpython.in\/wp-json\/wp\/v2\/posts\/986\/revisions"}],"predecessor-version":[{"id":1315,"href":"https:\/\/geekpython.in\/wp-json\/wp\/v2\/posts\/986\/revisions\/1315"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/geekpython.in\/wp-json\/wp\/v2\/media\/988"}],"wp:attachment":[{"href":"https:\/\/geekpython.in\/wp-json\/wp\/v2\/media?parent=986"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/geekpython.in\/wp-json\/wp\/v2\/categories?post=986"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/geekpython.in\/wp-json\/wp\/v2\/tags?post=986"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}