PIXELBANKv8.2.1
Menu

Inheritance and Polymorphism

Problem Statement

Implement a class hierarchy for shapes.

Background

Inheritance allows classes to share behavior:

class Child(Parent):
    def __init__(self):
        super().__init__()

Your Task

Create an inheritance hierarchy:

  1. Base class Shape with:

    • name attribute
    • area() method (returns 0)
    • str returning "{name}: area={area}"
  2. Rectangle(Shape) with:

    • Constructor taking width and height
    • area() returning width * height
  3. Circle(Shape) with:

    • Constructor taking radius
    • area() returning π * radius²

Example:

Input:
Rectangle(4, 5)
Output:
Rectangle: area=20
Reasoning:

4 * 5 = 20

Constraints:

  • Use math.pi for π
  • Round area to 2 decimal places in str
Editor

Test Results

0/0
Run code to see test results.