Coding Python
Home›Python Coding Games›Inheritance Island

Inheritance Island — Inheritance

Six levels on building from a class rather than copying it — with the hierarchy drawn as Python sees it.

6 levels · Intermediate
Level 1

Checks
    Your PythonPython 3
    1

    How to play

    Write the classes and press Run. Underneath, the hierarchy appears as a chain — Dog → Animal → object — in the exact order Python searches when it looks for a method. The last level has two parents, where that order stops being obvious and starts mattering.

    What each level teaches

    1. A kind of animal — a subclass starts with everything the parent has.
    2. Say it your way — overriding, and that the parent is left untouched.
    3. Ask the parent first — super(), to extend rather than repeat.
    4. What kind is it? — isinstance, and why you test specific-first.
    5. One loop, many animals — polymorphism, with isinstance explicitly banned.
    6. Two parents — multiple inheritance and the method resolution order.

    Why super() instead of naming the parent

    You could write Animal.__init__(self, name) and it would work today. super().__init__(name) is better for two reasons: it does not repeat the parent's name, so renaming or re-parenting the class does not break it; and with multiple inheritance it follows the MRO, which is the only way the chain works at all. Hard-coding the parent in a diamond hierarchy skips classes and initialises some of them twice.

    The level that bans isinstance

    Level 5 could be solved with a chain of type checks, and that is exactly what it blocks — because the polymorphic version is both shorter and open to extension:

    " ".join(a.speak() for a in animals)

    Add a Cow class tomorrow and this function needs no change at all. The isinstance version needs a new branch every single time, and will silently misbehave the day someone forgets. "Ask the object, do not interrogate its type" is most of what object-oriented design is actually for.

    Inheritance is easy to overuse

    It should mean "is a kind of": a Dog is an Animal. It should not mean "has some code I want" — for that, keep a reference to the other object and call it. Deep hierarchies make behaviour hard to find, because the method you are reading may be defined four classes up. Two levels is usually plenty. The written version, with properties and dataclasses, is in the classes and objects tutorial.

    Practise inheritance on your phone — the Coding Python app runs real Python 3 on your phone, with examples, quizzes and challenges built in.
    Get it free

    Frequently asked questions

    What does super() do in Python?

    It calls the next class along the method resolution order — usually the parent. super().__init__(name) runs the parent's setup so a subclass can add to it instead of repeating it.

    What is the method resolution order (MRO)?

    The order Python searches classes when looking for a method: the class itself, then its parents left to right, then theirs, ending at object. Android.__mro__ shows it, and this game draws it after every run.

    What is the difference between overriding and overloading?

    Overriding redefines a parent's method in a subclass, which Python does normally. Overloading — several versions of a method with different parameter types — does not exist in Python; use default arguments or *args instead.

    When should I use inheritance?

    When the subclass genuinely is a kind of the parent and should be usable wherever the parent is. If you only want to reuse some code, hold a reference to the other object and call it instead.

    Why avoid isinstance checks?

    Because each one is a branch you must remember to update when a new type appears. Calling the same method on every object lets each one answer for itself, and new types work with no change.

    Keep going

    ← All Python coding games  Read the tutorial →

    Take the games with you

    Coding Python packs a real Python 3 compiler, 30 example programs, 112 quiz questions and 12 self-checking challenges into one free Android app.

    Get it on Google Play
    Coding Python app icon