Editor

Override the describe() Method

Beginner · Write the code · from Python Inheritance: Subclasses, Polymorphism, and super()

From the lesson: Python Inheritance: Subclasses, Polymorphism, and super()

Think about how scientists classify animals. A Dog is a type of Mammal, which is a type of Animal. Each level inherits traits from the one above it: all animals breathe, all mammals are warm-blooded, and all dogs bark.

Read the lesson

Given the Shape class, create a Circle subclass that:

  • Takes radius in __init__ and calls super().__init__('Circle')
  • Overrides describe() to return 'Circle with radius <radius>'
  • Create a Circle with radius 5 and print describe().

    class Shape:
        def __init__(self, shape_type):
            self.shape_type = shape_type
        def describe(self):
            return f'I am a {self.shape_type}'
    class Shape:
    def __init__(self, shape_type):
    self.shape_type = shape_type
    def describe(self):
    return f'I am a {self.shape_type}'
    # Create your Circle subclass here

    More Inheritance exercises

    All Python exercises · 50 practice problems · Object-Oriented Programming quiz