🐍 Python for Everyone

← Back to exercises

Chapter 16 Practice Bank 70-90 min, hands-on

Practice Bank: Inheritance & Polymorphism

A deeper set of practice problems, organized by topic, for extra reps beyond the main exercises — including scenario-based problems written in the same style you'll meet in real interviews. This is the chapter where inheritance becomes allowed: class Child(Parent):, super().__init__(...), method overriding, polymorphism, and isinstance()/type() checks, on top of everything from Chapters 1-15 (including general classes, __init__, instance/class attributes, instance methods, dunder methods, @staticmethod, @classmethod, and @property). No import beyond math/datetime/random/csv.

How to run these Download starter.py below and run python3 starter.py from inside the same folder.

Topic 1: Basic Inheritance

1 A subclass with zero new code

Find # TODO 1.1. Write an Animal class and a Dog class inheriting from it, showing what a subclass gets automatically.

2 Inheriting a method unchanged

Find # TODO 1.2. Write a Vehicle class and a Car class that inherits its methods without redefining them.

3 Inheriting attributes

Find # TODO 1.3. Write an Employee class and a Manager class that inherits its attributes.

4 Inheriting an unmodified method

Find # TODO 1.4. Write a Shape class and a Square class that inherits an unmodified method.

5 Debug the Code

Find # TODO 1.5. This subclass definition forgot to put the parent class in parentheses. Fix it.

💼 Scenario

Modeling a fleet of vehicles

Find # TODO 1.A. Write a Vehicle base class and a Truck subclass that reuses it without any new code.

💼 Interview Prep

What does inheritance actually mean?

Find # TODO 1.B. Write explain_basic_inheritance(), explaining what "inheritance" means and what a subclass gets for free.

Topic 2: super().__init__()

1 Reusing parent setup, adding an attribute

Find # TODO 2.1. Write an Animal class and a Dog subclass that uses super().__init__() to reuse the parent's setup and adds a breed attribute.

2 A student extending a person

Find # TODO 2.2. Write a Person class and a Student subclass that uses super().__init__() plus a school attribute.

3 A rectangle extending a shape

Find # TODO 2.3. Write a Shape class and a Rectangle subclass that uses super().__init__() plus extra attributes.

4 A motorcycle extending a vehicle

Find # TODO 2.4. Write a Vehicle class and a Motorcycle subclass that uses super().__init__() plus a has_sidecar attribute.

5 Debug the Code

Find # TODO 2.5. This subclass __init__ forgot to call super().__init__(), leaving a parent attribute unset. Fix it.

💼 Scenario

Extending a base employee record

Find # TODO 2.A. Write an Employee class and a Manager subclass using super().__init__() plus a direct_reports attribute.

💼 Interview Prep

Why super().__init__() over copy-paste?

Find # TODO 2.B. Write explain_super_init_reuse(), explaining why super().__init__() is preferred over copy-pasting the parent's setup code.

Topic 3: Method Overriding

1 Fully replacing a parent method

Find # TODO 3.1. Write an Animal class with a speak method and a Cat subclass that overrides it.

2 Extending with super()

Find # TODO 3.2. Write a Shape class with a describe method and a Circle subclass that overrides and extends it with super().describe().

3 Extending an employee description

Find # TODO 3.3. Write an Employee class with a describe method and a Manager subclass that overrides and extends it.

4 Overriding a send method

Find # TODO 3.4. Write a Notification class with a send method and an SMSNotification subclass that overrides it.

5 Debug the Code

Find # TODO 3.5. This overriding method misspelled the parent method's name, defining a new method instead of overriding. Fix it.

💼 Scenario

Customizing a shared receipt formatter

Find # TODO 3.A. Write a Receipt class with a format_line method and a DiscountedReceipt subclass that overrides and extends it with super().

💼 Interview Prep

Overriding vs. extending

Find # TODO 3.B. Write explain_overriding_vs_extending(), explaining the difference between overriding a method and extending it with super().

Topic 4: Polymorphism

1 Same call, different behavior

Find # TODO 4.1. Write Cat and Dog subclasses of Animal, each overriding speak, then loop over a list of both and call speak() on each without checking type.

2 Totaling mixed shape areas

Find # TODO 4.2. Write Circle and Rectangle subclasses of Shape, each overriding area, then loop over a list of both to total their areas.

3 Describing mixed staff

Find # TODO 4.3. Write Manager and Developer subclasses of Employee, each overriding describe, then loop over a list of both and print each description.

4 Sending mixed notifications

Find # TODO 4.4. Write EmailNotification and SMSNotification subclasses of Notification, each overriding send, then loop over a list of both and collect each result.

5 Debug the Code

Find # TODO 4.5. This loop uses if/elif type-checks before calling a method, defeating the point of polymorphism. Fix it.

💼 Scenario

A payment processor supporting multiple payment types

Find # TODO 4.A. Write CardPayment and PayPalPayment subclasses of Payment, each overriding process, and process a mixed list polymorphically.

💼 Interview Prep

Why polymorphism beats if/elif chains

Find # TODO 4.B. Write explain_polymorphism_benefit(), explaining why polymorphism removes the need for type-checking if/elif chains.

Topic 5: isinstance() vs type()

1 Comparing the two checks

Find # TODO 5.1. Write an Animal class and a Dog subclass, then compare what isinstance(dog, Animal) returns versus type(dog) == Animal.

2 Counting membership in a mixed list

Find # TODO 5.2. Write a Shape class and a Circle subclass, then use isinstance() to safely check membership in a mixed list.

3 Subclass vs. exact-class matching

Find # TODO 5.3. Write a Vehicle class and a Car subclass, then show isinstance() returning True for both Car and Vehicle while type() only matches the exact class.

4 Filtering a mixed staff list

Find # TODO 5.4. Write an Employee class and a Manager subclass, then use isinstance() to filter a list down to only manager-type entries.

5 Debug the Code

Find # TODO 5.5. This code uses type(obj) == ParentClass to check membership, which fails for subclass instances. Fix it.

💼 Scenario

Validating mixed input before processing

Find # TODO 5.A. Write a function that uses isinstance() to accept both a Payment and any of its subclasses safely.

💼 Interview Prep

When to reach for isinstance() over type()

Find # TODO 5.B. Write explain_isinstance_vs_type(), explaining when you'd reach for isinstance() over type(), and why isinstance() is usually the safer choice.

Topic 6: Bringing It Together — Inheritance & Polymorphism in Production

1 Shapes with an overridden area and a polymorphic total

Find # TODO 6.1. Write a Shape base class and Rectangle/Circle subclasses combining super().__init__(), an overridden area method, and a polymorphic total.

2 Staff descriptions with isinstance() filtering

Find # TODO 6.2. Write an Employee base class and Manager/Developer subclasses combining super().__init__(), an overridden describe method, and isinstance() filtering.

3 A polymorphic notification loop

Find # TODO 6.3. Write a Notification base class and EmailNotification/SMSNotification subclasses combining super().__init__(), an overridden send method, and a polymorphic loop.

4 A vehicle fleet with isinstance()-based categorization

Find # TODO 6.4. Write a Vehicle base class and Car/Truck subclasses combining super().__init__(), an overridden describe method, and isinstance()-based categorization.

5 Debug the Code

Find # TODO 6.5. This subclass's overriding method entirely skips calling super(), silently dropping shared parent behavior it was supposed to extend. Fix it.

💼 Scenario

A small media library

Find # TODO 6.A. Write a MediaItem base class and Book/Movie subclasses combining super().__init__(), an overridden summary method, and a polymorphic report.

💼 Interview Prep

Inheritance vs. composition

Find # TODO 6.B. Write explain_inheritance_vs_composition(), describing a real system where inheritance was the right tool, and a case where composition would have been the better choice.

Checking your work Compare your output against solution.py. Your exact wording doesn't need to match — the goal is that your program runs without errors and does what each TODO asks.
← Exercises Challenges →