📘
Inheritance and Polymorphism
MediumPython OOP
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:
-
Base class Shape with:
- name attribute
- area() method (returns 0)
- str returning "{name}: area={area}"
-
Rectangle(Shape) with:
- Constructor taking width and height
- area() returning width * height
-
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
Python 3.13.1
Test Results
0/0Run code to see test results.