Python OOP Guide: Abstract Polygons Edition
Core Concepts To Be Used
● Abstract Base Class (ABC): Base class can't be used directly; it defines a
contract.
● Polymorphism: Each subclass implements its own version of a shared method.
● Inheritance: Specific shapes are derived from a generic geometric base.
Abstract Class: Polygon
● A blueprint for polygon-like shapes.
● Requires the area() method to be implemented by subclasses.
● Holds a common method (display_sides()) and a shared property (sides).
Subclasses: Specific Polygons
Shape Description Sides Area Formula
Triangle Uses base and height 3 ½ × base × height
Rectangle Uses length and width 4 length × width
Pentagon Regular shape with 5 ½ × perimeter × apothem
apothem & side length
Each subclass uses super().__init__(n) to declare number of sides via the
parent class.
Method Override: area()
● Must be implemented in each subclass.
● It's where the shape-specific logic goes.
● Ensures consistent interface for computing area.
Common Method: display_sides()
● Shared by all shapes from the base class.
● Displays how many edges the polygon has.
User Interaction (Console Menu)
1. Prompts the user to choose a shape (1-3).
2. Based on input, asks for dimensions.
3. Creates the appropriate shape object.
4. Call the common methods (display_sides() and area()).
Polymorphic Behavior
● Regardless of which shape the user selects, the program calls the same
methods on the object.
● Python dynamically figures out which version (triangle, rectangle, etc.) to run —
this is runtime polymorphism.
Quick Tips
● Can't instantiate Polygon directly — it’s abstract.
● @abstractmethod = must be implemented in derived class.
● Always call super() when using inheritance to initialize base class attributes.