Browse OOP reference
Types and contracts

What are abstract classes?

On this page

An abstract class defines a common base that may combine reusable code with operations subclasses must provide. A template importer can supply the read-and-validate sequence while subclasses parse different formats. Put a step in the base only when every supported importer shares its ordering and failure semantics.

Worked example

Python
from abc import ABC, abstractmethod

class Importer(ABC):
    def load(self, raw):
        return self.parse(raw.strip())

    @abstractmethod
    def parse(self, raw):
        pass

class CountImporter(Importer):
    def parse(self, raw):
        return int(raw)

assert CountImporter().load(" 12 ") == 12

Python abstract base classes

Explore class relationships in the OOP Playground

What is inheritance?What is message passing?What is duck typing?

All OOP questions

Search ObjectFAQ