3 min read

Understanding Object-Oriented Programming Through Game of Thrones

Learn OOP concepts like classes, inheritance, encapsulation, and polymorphism using the Seven Kingdoms as your classroom

OOP is a style of programming where we organize code using objects just like Westeros is organized into houses, characters and regions. These objects have attributes (like a character’s name or house) and methods (things they can do, like swing a sword or burn down cities).

In OOP, everything starts with a class a blueprint. Then we use that blueprint to create objects, aka real, functioning characters.

1. Class & Object — Blueprints and Bastards

Let’s say you want to create some characters for your game (or battle for the Iron Throne). You’d start by defining a class:

class Character:
    def __init__(self, name, house):
        self.name = name
        self.house = house

Now, to bring someone to life, you create an object:

jon = Character("Jon Snow", "Stark")

🎭 Translation:

  • Character = blueprint (class)
  • jon = real person (object)
  • name and house = attributes

You can create as many characters as you want from this one class. It’s like cloning, but more ethical.

So Jon is an instance of the Character class, and yes, he knows nothing for now.

2. Encapsulation — Keeping Family Secrets

Encapsulation is the idea of hiding private info. and only exposing what’s necessary. Think of it as putting sensitive data behind a locked doors.

class Character:
    def __init__(self, name):
        self.name = name
        # Private
        self.__real_parents = "Rhaegar Targaryen & Lyanna Stark"

    def reveal_parents(self):
        return self.__real_parents

Now, if someone tries to access jon.__real_parents, they’ll get an error. Because that’s private information.

Instead, they’d have to go through the official channel:

jon = Character("Jon Snow")
print(jon.reveal_parents())  # Output: Rhaegar Targaryen & Lyanna Stark

🕵️‍♂️ Just like in Westeros, secrets can’t stay buried forever but at least you can control who finds out and how.

3. Inheritance — Traits Passed Down Through Houses

If there’s one thing the noble families in Game of Thrones love, it’s inheritance. Lands, swords, dragons, facial expressions — everything gets passed down.

In OOP, inheritance lets you create a new class (called a subclass) that takes on the properties and methods of another class (the parent class).

Let’s make a Stark class:

class Stark(Character):
    def __init__(self, name):
        super().__init__(name, "Stark")
        self.wolf = True

Now, you can create Stark characters without repeating yourself:

arya = Stark("Arya Stark")
print(arya.house)   # Stark
print(arya.wolf)    # True

🧬 Arya inherits everything from the Character class and adds her own things.

4. Polymorphism — Same Method but Different Behavior

Polymorphism means that different objects can use the same method name, but each one behaves differently.

Let’s say every house has a battle cry:

class Character:
    def battle_cry(self):
        return "For Westeros!"

class Stark(Character):
    def battle_cry(self):
        return "The North Remembers."

class Targaryen(Character):
    def battle_cry(self):
        return "Dracarys."

Now let’s see what happens:

jon = Stark("Jon Snow")
dany = Targaryen("Daenerys Targaryen")

print(jon.battle_cry())  # The North Remembers.
print(dany.battle_cry()) # Dracarys.

⚔️ Same method name: battle_cry()

🔥 Different results depending on who’s calling it. Just like in Westeros — everyone shows up to battle, but their reasons (and methods) are wildly different.

Bonus: ⚙️ Methods = What Characters Can Do

Let’s add a method to let characters speak:

class Character:
    def speak(self):
        return f"My name is {self.name} of House {self.house}."

Now, if Jon speaks:

jon = Character("Jon Snow", "Stark")
print(jon.speak())

# Output
# "My name is Jon Snow of House Stark."

Well, kind of. Technically he’s a Targaryen. But he’s humble, so we’ll let it slide.

Conclusion

When you think about it, writing code with OOP is a lot like writing the next Game of Thrones:

  • You create characters with backstories.
  • They inherit traits from their houses.
  • They behave in different ways, even if they share names or titles.
  • And there’s always a secret or two tucked away in the shadows.

🙌 Thanks for Reading

Hope this post gave you some ideas on how to rethink and better understand Object-Oriented Programming using something fun and familiar.

If you enjoyed this, feel free to connect, share feedback or ⭐️ star the repo!

Related Articles