{"id":447,"date":"2023-02-15T11:48:57","date_gmt":"2023-02-15T11:48:57","guid":{"rendered":"https:\/\/blogs.glowscotland.org.uk\/es\/software\/?page_id=447"},"modified":"2023-02-15T15:59:26","modified_gmt":"2023-02-15T15:59:26","slug":"class-inheritance","status":"publish","type":"page","link":"https:\/\/blogs.glowscotland.org.uk\/es\/software\/software-design-and-development\/week-3\/class-inheritance\/","title":{"rendered":"Class inheritance"},"content":{"rendered":"\n<p>Inheritance is a property that allows a class to build on another class by inheriting the variables and methods (functions) from the parent class. The prevents replicating code which would cause added complexity any time the code required testing or changing.<\/p>\n\n\n\n<p>You can use the&nbsp;<strong>super()<\/strong>&nbsp;function to access the functions of the parent function. This means that your&nbsp;<strong>__init__()<\/strong>&nbsp;function only needs to update the new variables in the child class and then just pass the other ones to the parent class\u2019s&nbsp;<strong>__init__()<\/strong>&nbsp;function.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class TestClass(object):\n    def __init__(self, first_var, second_var):\n        self.x = first_var\n        self.y = second_var\n\nclass AnotherClass(TestClass):\n    def __init__(self, a, b, x, y):\n        self.a = a\n        self.b = b\n        super().__init__(x, y)\n\ntest_variable = TestClass(30, 60)\nprint(test_variable.x)\n\nanother_test_variable = AnotherClass(100, 200, 500, 600)\nprint(another_test_variable.x)\nprint(another_test_variable.a)<\/code><\/pre>\n\n\n\n<p>Here we can see the AnotherClass class inherits the TestClass as a parent. How do we see this? Look at the class definition line. Instead of having&nbsp;<strong>object&nbsp;<\/strong>in the brackets it has&nbsp;<strong>TestClass<\/strong>. The&nbsp;<strong>object&nbsp;<\/strong>just meant it was inheriting the default Python class, whereas&nbsp;<strong>TestClass&nbsp;<\/strong>means it is inheriting the TestClass as its parent. And because TestClass inherits the default class then AnotherClass also gets those. The child class gets everything the parent class has.<\/p>\n\n\n\n<p>In the AnotherClass&nbsp;<strong>__init__()<\/strong>&nbsp;function we can see that it only overrides the a and b variables. It just passes x and y into the&nbsp;<strong>super().__init__()<\/strong>&nbsp;function. In other words it calls the parent class\u2019s __init__() function and sends it the values that it needs.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Example<\/h2>\n\n\n\n<p>Let\u2019s alter our game weapon example.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class Weapon(object):\n    def __init__(self, name, damage_type, damage, speed):\n        self.name = name\n        self.damage_type = damage_type\n        self.damage = damage\n        self.speed = speed\n\nshort_sword = Weapon(\"Short Sword\", \"Piercing\", 5, 10)\nbattle_axe = Weapon(\"Battle Axe\", \"Slashing\", 20, 3)\n\nprint(short_sword.name,\"does\",short_sword.damage,\"damage\")\n\nclass MagicWeapon(Weapon):\n    def __init__(self, name, damage_type, damage, speed, effect):\n        super().__init__(name, damage_type, damage, speed)\n        self.effect = effect\n\nmagic_spear = MagicWeapon(\"Magic Spear\", \"Piercing\", 25, 7, \"Fire\")\nprint(magic_spear.name, \"has a magical\", magic_spear.effect, \"effect\")<\/code><\/pre>\n\n\n\n<p>Running this would give an output of:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Short Sword does 5 damage\nMagic Spear has a magical Fire effect<\/code><\/pre>\n\n\n\n<p>The original Weapon class remains untouched and therefore will not break any code that refers to it.<\/p>\n\n\n\n<p class=\"nextlink\"><a href=\"https:\/\/blogs.glowscotland.org.uk\/es\/software\/software-design-and-development\/week-3\/methods\/\" data-type=\"page\" data-id=\"453\">Next: Methods<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Inheritance is a property that allows a class to build on another class by inheriting the variables and methods (functions) from the parent class. The prevents replicating code which would cause added complexity any time the code required testing or changing. You can use the&nbsp;super()&nbsp;function to access the functions of the parent function. This means&hellip; <a class=\"more-link\" href=\"https:\/\/blogs.glowscotland.org.uk\/es\/software\/software-design-and-development\/week-3\/class-inheritance\/\">Continue reading <span class=\"screen-reader-text\">Class inheritance<\/span><\/a><\/p>\n","protected":false},"author":5710,"featured_media":0,"parent":18,"menu_order":5,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-447","page","type-page","status-publish","hentry","entry"],"_links":{"self":[{"href":"https:\/\/blogs.glowscotland.org.uk\/es\/software\/wp-json\/wp\/v2\/pages\/447","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blogs.glowscotland.org.uk\/es\/software\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/blogs.glowscotland.org.uk\/es\/software\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/blogs.glowscotland.org.uk\/es\/software\/wp-json\/wp\/v2\/users\/5710"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.glowscotland.org.uk\/es\/software\/wp-json\/wp\/v2\/comments?post=447"}],"version-history":[{"count":4,"href":"https:\/\/blogs.glowscotland.org.uk\/es\/software\/wp-json\/wp\/v2\/pages\/447\/revisions"}],"predecessor-version":[{"id":455,"href":"https:\/\/blogs.glowscotland.org.uk\/es\/software\/wp-json\/wp\/v2\/pages\/447\/revisions\/455"}],"up":[{"embeddable":true,"href":"https:\/\/blogs.glowscotland.org.uk\/es\/software\/wp-json\/wp\/v2\/pages\/18"}],"wp:attachment":[{"href":"https:\/\/blogs.glowscotland.org.uk\/es\/software\/wp-json\/wp\/v2\/media?parent=447"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}