Python Tutorial: Inheritance
Build on top of existing classes. Extend, override, and reuse without copying code.
Build on what already exists
You have a `User` class. Now you need `AdminUser` — just like `User` but with extra powers.
Inheritance lets a new class inherit everything from an existing one, then add or change only what is different. Ten steps.
What you'll learn in this Python inheritance tutorial
This interactive Python tutorial has 6 hands-on exercises. Estimated time: 20 minutes.
- Don't copy the class — extend it — You have a `User` class. Now you need `AdminUser` — exactly like `User`, but with extra powers.
- super() — extend the parent's setup — When `AdminUser` needs its own `__init__` (for extra attributes), use `super()` to run the parent's setup first:
- Override — replace the parent's method — A child class can replace a parent's method by defining a method with the same name.
- isinstance() — 'is this one of those?' — `isinstance(obj, Class)` is True if the object is an instance of that class — or any subclass.
- Multiple inheritance — Python lets a class inherit from multiple parents at once:
- Build an animal hierarchy — Final challenge — no starter code.
Python Inheritance concepts covered
- Build on what already exists