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:
- Create an abstract base class named
Shape. - The
Shapeclass should have an abstract method calledarea(). This method should raise aNotImplementedErrorif called directly on theShapeclass. - Create three concrete classes:
Circle,Rectangle, andTriangle. - Each concrete class should inherit from
Shapeand implement thearea()method with the appropriate calculation for its shape. - Attempting to instantiate the
Shapeclass directly should result in an error.
Key requirements:
- Use the
abcmodule to define the abstract base class. - The
area()method must return a numerical value (float or int) representing the area of the shape. - The
Shapeclass should not be directly instantiable.
Expected behavior:
- Instantiating
Circle,Rectangle, andTriangleshould be successful. - Calling
area()on instances ofCircle,Rectangle, andTriangleshould return the correct area. - Calling
area()directly on theShapeclass should raise aNotImplementedError. - Attempting to instantiate
Shapeshould raise aTypeError.
Edge cases to consider:
- What happens if a subclass doesn't implement the
area()method? (It should raise aTypeErrorwhen 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
Shapeclass must be an abstract base class. - The
area()method must be an abstract method.
Notes
- The
abcmodule provides the necessary tools for creating abstract base classes. Look intoabc.ABCand@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.