0% found this document useful (0 votes)
6 views2 pages

Python OOP Guide Abstract Polygons Edition (1)

The document outlines a Python OOP guide focused on abstract polygons, emphasizing core concepts such as Abstract Base Classes, polymorphism, and inheritance. It introduces an abstract class 'Polygon' that requires subclasses to implement the area() method while sharing a common method display_sides(). User interaction is facilitated through a console menu that prompts for shape selection and dimensions, demonstrating polymorphic behavior in method calls.

Uploaded by

22-05068
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views2 pages

Python OOP Guide Abstract Polygons Edition (1)

The document outlines a Python OOP guide focused on abstract polygons, emphasizing core concepts such as Abstract Base Classes, polymorphism, and inheritance. It introduces an abstract class 'Polygon' that requires subclasses to implement the area() method while sharing a common method display_sides(). User interaction is facilitated through a console menu that prompts for shape selection and dimensions, demonstrating polymorphic behavior in method calls.

Uploaded by

22-05068
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

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.

You might also like