Command Palette

Search for a command to run...

Hectal
Back to the lesson: Topic 8.5 — Inheritance and super()
Python · Example 1 of 3

Animals: override, super and isinstance

Add a Bird class with its own speak and a loop that prints every pet's intro. Notice how the same call works for each class.

Inheritance lets a new class (the subclass) start from an existing class (the parent) and add or change only what is different. super() reaches the parent's version of a method, and isinstance and issubclass ask whether something belongs to a family of classes.

Change the code and press Run (Ctrl+Enter). Predict what will happen first, then break it on purpose and read what changes. Your edits are saved and match the lesson page.

Ready for more? This phase has 6 hands-on exercises with automatic checks.

Practice questions

Write the code in the editor, run it, then open the model answer to compare.

01

Write a Shape class with a method area() that returns 0, and subclasses Square(side) and Circle(radius) that override it (use 3.14). Print the areas of a list of one of each.

02

Write a Vehicle with __init__(self, wheels), and a Car(Vehicle) whose __init__ takes brand, calls super().__init__(4), and stores the brand. Print car.wheels and car.brand.

03

Use isinstance to write describe(x) that returns "number" for ints and floats, "text" for strings and "other" for anything else. Test describe(True) and explain the result.

Explain it without notes

01

What is inheritance, and what does class Dog(Animal) mean?

02

What does super() do, and why call super().__init__() in a subclass?

03

What is polymorphism? Give an example with inheritance.

04

When should you not use inheritance?

Animals: override, super and isinstance
Sign in to run this example in your browser.

Expected output

Rex says Woof
Tom says Meow
Thing says ...
True True False
True False
False True
sit
Animal
True True