Python Inheritance · Exercises
Object-Oriented Python

Python Inheritance Exercises

4 exercises that build on the Python Inheritance lesson, from easy to hard. Each one is checked in your browser when you press Check. Stuck? Use the hint before peeking at the solution.

What this lesson covers: Inheritance lets a new class (the child) start with all the attributes and methods of an existing class (the parent) and then add to them or change them, so shared code is written only once. By the end you can create a child class, run the parent's init with super(), override a method, call the same method on different kinds of objects (polymorphism), check types with isinstance(), and mark internal attributes with a leading underscore. You will practise: Create a Cat class; Fix the savings account; Predict the greeting; Shapes with polymorphism.

0 / 4 completed

Python Inheritance practice exercises

1. Create a Cat class

Write code Easy

Create a class Cat that inherits from Animal and overrides speak() so it returns "<name> says meow" — for example "Tom says meow". Do not change Animal.

Your code
class Animal:
    def __init__(self, name):
        self.name = name

    def speak(self):
        return f"{self.name} makes a sound"

# create class Cat here


kitty = Cat("Tom")
print(kitty.speak())

2. Fix the savings account

Fix the bug Medium

SavingsAccount should have an owner, a balance that starts at 0 and an interest rate. The program crashes with an AttributeError. Fix the child class so the parent's setup runs.

Your code
class Account:
    def __init__(self, owner):
        self.owner = owner
        self.balance = 0

class SavingsAccount(Account):
    def __init__(self, owner, rate):
        self.rate = rate

    def add_interest(self):
        self.balance += self.balance * self.rate

savings = SavingsAccount("Asha", 0.05)
savings.balance = 200
savings.add_interest()
print(savings.owner, savings.balance)

3. Predict the greeting

Predict the output Medium

welcome() is only defined in Base, but it calls self.greet(). Type exactly what the program prints.

Read this code
class Base:
    def greet(self):
        return "Hello"

    def welcome(self):
        return self.greet() + "!"

class Friendly(Base):
    def greet(self):
        return super().greet() + " friend"

print(Base().welcome())
print(Friendly().welcome())
print(isinstance(Friendly(), Base), isinstance(Base(), Friendly))

4. Shapes with polymorphism

Write code Hard

Using the Shape class given:

  1. Create Rectangle(width, height) that inherits from Shape, calls super().__init__("Rectangle") and overrides area().
  2. Create Circle(radius) that inherits from Shape, calls super().__init__("Circle") and overrides area() using math.pi.
  3. Write a function total_area(shapes) that returns the sum of the areas of any list of shapes (0 for an empty list).
Your code
import math

class Shape:
    def __init__(self, name):
        self.name = name

    def area(self):
        return 0

    def describe(self):
        return f"{self.name} with area {self.area():.2f}"

# 1. class Rectangle(Shape): ...
# 2. class Circle(Shape): ...
# 3. def total_area(shapes): ...


shapes = [Rectangle(3, 4), Circle(1)]
for s in shapes:
    print(s.describe())
print(round(total_area(shapes), 2))

Next lessonPython Polymorphism and Duck Typing

More Python Inheritance examples · All Python exercises