Share Behaviour Through an Abstract Class

Write the code, see it run, pass the checks.

The problem

  • easy
  • Code

Write an abstract class Shape with an abstract name and an abstract area(), and a describe() method that returns the name, then "with area", then the area to 2 decimal places ("circle with area 3.14"). Then write Circle(radius) and Rect(width, height), which extend it. Shape itself must not be creatable with new, a subclass that forgets area() must be a type error, and sizes must be numbers.

Your code must pass

  • Works out each shape's area
  • describe uses the subclass's name and area
  • Has no type errors
  • Shape can't be created directly
  • A subclass must implement area
  • Sizes must be numbers