Objective
An abstract class (abc.ABC with @abstractmethod) defines a contract: it forces subclasses to implement certain methods and cannot be instantiated directly. Each shape computes its area in its own way, and the code using them does not need to know which is which (polymorphism).
Instructions
- Define the abstract class
Shapewith the abstract methodarea()and the subclassesCircle(radius),Square(side) andTriangle(base and height). - The program tries to create
Shape()and, when it fails withTypeError, showsCannot instantiate Shape. Then it readsnandnlineskind measures(for examplecircle 2ortriangle 3 4); showName: areaper line and at the endTotal: x(2 decimals).
2
circle 1
square 2
Cannot instantiate Shape
Circle: 3.14
Square: 4.00
Total: 7.14
Open the editor in full screen: the statement on the left and your code, the output and the tests on the right.
Start exercise