Abstract Shape Hierarchy
Create an abstract class Shape with an abstract method area(). Implement Circle and Square subclasses, each providing their own area() calculation, demonstrating polymorphism.
What the problem means: Shape defines that every shape MUST have an area() method, without saying how to calculate it — each concrete shape (Circle, Square) fills in that detail its own way.
Approach: import ABC and abstractmethod from abc; mark Shape's area() with @abstractmethod (no real implementation); Circle and Square each provide their own working area() formula.
Input: Two lines: a circle's radius, then a square's side length.
Output: Two lines: the circle's area, then the square's area.
5 4
78.53975 16
- 0 <= radius, side <= 10^4
Hint 1
Shape(ABC) with an @abstractmethod area() means Shape itself can never be instantiated directly.
Hint 2
Circle.area() is 3.14159 * self.radius ** 2.
Hint 3
Square.area() is self.side * self.side.
Shape(ABC) with an @abstractmethod area() defines a required interface without an implementation — Shape itself can't be instantiated, and any subclass that skips implementing area() can't either. Circle and Square each supply their own area() formula, and calling .area() on either object automatically runs the correct version for that object's actual type — that's polymorphism.