๐ŸฅŠ Python Inheritance & Methods: PowerSlap Edition#

Note: I would highly recommend that nobody in the class participate in this crazy sport. Itโ€™s dangerous and you could get hurt, however, it is somewhat curious what people will do for sport, fame, and money.

๐Ÿš€ Introduction#

Inheritance is a powerful feature in object-oriented programming that enables code reuse, better structure, and logical hierarchies. This chapter explores how inheritance works in Python, demonstrating its practical applications using a PowerSlap Fighters analogy and real-world coding examples.

By the end of this chapter, you will:

  • Understand the basics of inheritance and why itโ€™s useful.

  • Learn how to extend classes using super().

  • Differentiate between instance methods and class methods.

๐Ÿ† What is Inheritance?#

Inheritance allows us to create a base class (parent) and extend it with specialized child classes. This promotes code reuse and modularity.

Example: PowerSlap Fighters ๐ŸฅŠ#

In our scenario, we define a base class Fighter, and then create specialized fighters with unique abilities:

  • Fighter (Base Class)

  • Slapper (Child Class)

  • HeavyHitter (Child Class)

Class Hierarchy Diagram#

classDiagram
Fighter <|-- Slapper
Fighter <|-- HeavyHitter
class Fighter{
+name
+power
+durability
+introduce()
}
class Slapper{
+accuracy
+slap()
}
class HeavyHitter{
+stamina
+knockout_slap()
}

This structure ensures code reusability while allowing each fighter to have unique attributes and behaviors.

๐Ÿ”ฅ Creating a Base Fighter Class#

The base class Fighter defines common properties for all fighters.

class Fighter:
    def __init__(self, name, power, durability):
        self.name = name  # Fighter's name
        self.power = power  # Slap power level
        self.durability = durability  # Ability to withstand damage

    def introduce(self):
        """Introduce the fighter."""
        return f"๐Ÿ‘Š {self.name} enters the PowerSlap arena!"

Key Features:#

  • init() initializes the fighter attributes.

  • introduce() provides a basic introduction method.

๐Ÿ’ฅ Creating Specialized Fighters with Inheritance#

We extend Fighter to create two specialized fighter types: Slapper and HeavyHitter.

The Slapper Class#

class Slapper(Fighter):
    def __init__(self, name, power, durability, accuracy):
        super().__init__(name, power, durability)  # Inherit from Fighter
        self.accuracy = accuracy  # Additional attribute

    def slap(self):
        return f"๐Ÿ–๏ธ {self.name} delivers a {self.power} power slap with {self.accuracy}% accuracy!"

The HeavyHitter Class#

class HeavyHitter(Fighter):
    def __init__(self, name, power, durability, stamina):
        super().__init__(name, power, durability)  # Inherit from Fighter
        self.stamina = stamina  # Additional attribute

    def knockout_slap(self):
        return f"๐Ÿ’ฅ {self.name} goes for a brutal {self.power} power KO slap with {self.stamina}% stamina left!"

Key Features:#

  • super().init() calls the parent class constructor to avoid redundant code.

  • Each subclass inherits attributes and methods from Fighter while adding new ones.

โšก Using Our Fighter Classes#

Letโ€™s create and test our fighter classes:

slap_fighter = Slapper("John 'Quick Hand' Doe", 80, 60, 90)
heavy_fighter = HeavyHitter("Mike 'Iron Palm' Tyson", 100, 90, 70)

print(slap_fighter.introduce())  # ๐Ÿ‘Š John 'Quick Hand' Doe enters the PowerSlap arena!
print(
    slap_fighter.slap()
)  # ๐Ÿ–๏ธ John 'Quick Hand' Doe delivers an 80 power slap with 90% accuracy!

print(
    heavy_fighter.introduce()
)  # ๐Ÿ‘Š Mike 'Iron Palm' Tyson enters the PowerSlap arena!
print(
    heavy_fighter.knockout_slap()
)  # ๐Ÿ’ฅ Mike 'Iron Palm' Tyson goes for a brutal 100 power KO slap with 70% stamina left!
๐Ÿ‘Š John 'Quick Hand' Doe enters the PowerSlap arena!
๐Ÿ–๏ธ John 'Quick Hand' Doe delivers a 80 power slap with 90% accuracy!
๐Ÿ‘Š Mike 'Iron Palm' Tyson enters the PowerSlap arena!
๐Ÿ’ฅ Mike 'Iron Palm' Tyson goes for a brutal 100 power KO slap with 70% stamina left!

๐Ÿ“Š Applying Inheritance Beyond Fighters#

Inheritance is widely used in various real-world applications, including graphing systems and optimization problems.

Example: Graphing System with Inheritance#

We can create a base Graph class and extend it to support different graph types.

import matplotlib.pyplot as plt

class Graph:
    def __init__(self, title):
        self.title = title

    def show_title(self):
        return f"๐Ÿ“ˆ Graph: {self.title}"
    
    def show_caption(self, caption):
        return f"๐Ÿ“Š {caption}"


class LineGraph(Graph):
    def __init__(self, title, x_values, y_values):
        super().__init__(title)
        self.x_values = x_values
        self.y_values = y_values

    def plot(self):
        plt.plot(self.x_values, self.y_values)
        plt.title(self.title)
        plt.show()

Using Our Graph Classes#

line_chart = LineGraph("Stock Prices", [1, 2, 3, 4], [100, 150, 200, 250])
print(line_chart.show_title())  # ๐Ÿ“ˆ Graph: Stock Prices
line_chart.plot()  # Displays a line chart
print(line_chart.show_caption("This is my caption"))
๐Ÿ“ˆ Graph: Stock Prices
../../_images/e76cbeb95ddf00df9222b9560abb6d0dca54b8d69167220648cd9dcc7b98fef9.png
๐Ÿ“Š This is my caption

๐Ÿ“Œ Key Takeaways#

โœ… Inheritance allows us to create a base class (Fighter) and specialized versions (Slapper, HeavyHitter).

โœ… super() calls the parent constructor, keeping code clean and reusable.

โœ… Child classes can have additional attributes & methods beyond the parent class.

โœ… Inheritance is widely used in graphing, optimization, and structured modeling.

๐ŸฅŠ PowerSlap fighters inherit raw powerโ€”just like Python classes inherit code!