Section 1/4Why Abstract Classes?
Why Abstract Classes?
Imagine a Shape class. "Shape" is too generic to have a meaningfularea() — but every concrete shape (rectangle, circle, triangle) must be able to calculate its area. An abstract class enforces this rule.
Abstract Classes
An abstract class is a class that cannot be instantiated directly. It defines a contract — a set of methods that every subclass must implement. This ensures all subclasses share a consistent interface.
Why Abstract Classes?
Imagine a Shape class. "Shape" is too generic to have a meaningfularea() — but every concrete shape (rectangle, circle, triangle) must be able to calculate its area. An abstract class enforces this rule.
from abc import ABC, abstractmethod
class Shape(ABC): # inherits from ABC
@abstractmethod
def area(self): pass # no implementation — just a contract
# Shape() → TypeError: Can't instantiate abstract class