Hone logo
Hone
Problems

Designing a Shape Hierarchy with Abstract Base Classes

This challenge focuses on utilizing abstract base classes (ABCs) in Python to define a structured hierarchy of shapes. ABCs enforce a contract that subclasses must adhere to, ensuring consistency and preventing the instantiation of incomplete shape definitions. This is a fundamental concept in object-oriented programming, promoting code reusability and maintainability.

Problem Description

You are tasked with designing a system for representing different geometric shapes. The system should allow for shapes like Circle, Rectangle, and Triangle to be defined, each with its own specific attributes and methods. However, you want to ensure that all shapes have a common interface, specifically a method to calculate their area. You will achieve this by defining an abstract base class Shape that enforces the presence of an area() method in all its subclasses.

What needs to be achieved:

  1. Create an abstract base class named Shape.
  2. The Shape class should have an abstract method called area(). This method should raise a NotImplementedError if called directly on the Shape class.
  3. Create three concrete classes: Circle, Rectangle, and Triangle.
  4. Each concrete class should inherit from Shape and implement the area() method with the appropriate calculation for its shape.
  5. Attempting to instantiate the Shape class directly should result in an error.

Key requirements:

  • Use the abc module to define the abstract base class.
  • The area() method must return a numerical value (float or int) representing the area of the shape.
  • The Shape class should not be directly instantiable.

Expected behavior:

  • Instantiating Circle, Rectangle, and Triangle should be successful.
  • Calling area() on instances of Circle, Rectangle, and Triangle should return the correct area.
  • Calling area() directly on the Shape class should raise a NotImplementedError.
  • Attempting to instantiate Shape should raise a TypeError.

Edge cases to consider:

  • What happens if a subclass doesn't implement the area() method? (It should raise a TypeError when instantiated).
  • Ensure the area calculations are correct for each shape.

Examples

Example 1:

Input:  A Circle with radius 5
Output: 78.53981633974483
Explanation: The area of a circle is calculated as pi * radius^2.

Example 2:

Input: A Rectangle with width 4 and height 6
Output: 24.0
Explanation: The area of a rectangle is calculated as width * height.

Example 3:

Input: A Triangle with base 8 and height 3
Output: 12.0
Explanation: The area of a triangle is calculated as 0.5 * base * height.

Constraints

  • All area calculations must return a float.
  • The code must be well-structured and readable.
  • The Shape class must be an abstract base class.
  • The area() method must be an abstract method.

Notes

  • The abc module provides the necessary tools for creating abstract base classes. Look into abc.ABC and @abc.abstractmethod.
  • Think about how to ensure that subclasses properly implement the required methods.
  • Consider using docstrings to explain the purpose of each class and method.
  • This problem is designed to test your understanding of abstract base classes and their role in enforcing a common interface.
Loading editor...
python