0
0
Pythonprogramming~10 mins

Abstract base classes overview in Python - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Abstract base classes overview
Define ABC with abstract methods
Create subclass inheriting ABC
Implement all abstract methods
Instantiate subclass object
Use subclass object normally
If abstract methods missing -> Error
This flow shows how to define an abstract base class (ABC), create a subclass that implements all abstract methods, then instantiate and use the subclass. Missing implementations cause errors.
Execution Sample
Python
from abc import ABC, abstractmethod

class Animal(ABC):
    @abstractmethod
    def sound(self):
        pass

class Dog(Animal):
    def sound(self):
        return "Woof!"

dog = Dog()
print(dog.sound())
Defines an abstract base class Animal with an abstract method sound, then a Dog subclass that implements sound.
Execution Table
StepActionEvaluationResult
1Define class Animal inheriting ABCAnimal has abstract method soundAnimal cannot be instantiated
2Define class Dog inheriting AnimalDog must implement soundDog class created
3Instantiate Dog()Dog has sound implementedDog instance created successfully
4Call dog.sound()sound method runs"Woof!" returned
5Try instantiate Animal()Animal has abstract method unimplementedTypeError: Can't instantiate abstract class Animal with abstract methods sound
💡 Execution stops when trying to instantiate abstract class Animal without implementing abstract methods
Variable Tracker
VariableStartAfter Step 2After Step 3After Step 4
AnimalClass with abstract methodUnchangedUnchangedUnchanged
DogNot definedClass defined with sound implementedUnchangedUnchanged
dogNot definedNot definedInstance of Dog createdUnchanged
dog.sound()Not definedNot definedNot defined"Woof!"
Key Moments - 3 Insights
Why can't we create an instance of the abstract base class Animal?
Because Animal has an abstract method sound that is not implemented, Python prevents instantiation to enforce subclass implementation (see execution_table step 5).
What happens if the subclass Dog does not implement the abstract method sound?
Python will raise a TypeError when trying to instantiate Dog, because all abstract methods must be implemented (refer to execution_table step 3).
Why do we use @abstractmethod decorator in the base class?
It marks methods that must be implemented by subclasses, ensuring a consistent interface (see execution_table step 1).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what happens at step 3 when Dog() is instantiated?
ATypeError is raised because Dog does not implement sound
BDog instance is created successfully because sound is implemented
CNothing happens, Dog remains a class
DAnimal instance is created instead
💡 Hint
Check execution_table row 3 for Dog instantiation result
At which step does calling dog.sound() return 'Woof!'?
AStep 2
BStep 3
CStep 4
DStep 5
💡 Hint
Look at execution_table row 4 for method call and return value
If we remove @abstractmethod decorator from Animal.sound, what changes in the execution?
AAnimal can be instantiated without error
BDog must still implement sound
CDog cannot be instantiated
DNo change at all
💡 Hint
Refer to execution_table step 5 where instantiating Animal raises error due to abstractmethod
Concept Snapshot
Abstract base classes (ABC) define methods subclasses must implement.
Use @abstractmethod to mark these methods.
Cannot instantiate ABC directly if abstract methods are unimplemented.
Subclasses must implement all abstract methods to be instantiated.
This enforces a consistent interface across subclasses.
Full Transcript
This visual execution trace shows how abstract base classes (ABC) work in Python. First, we define an ABC called Animal with an abstract method sound using the @abstractmethod decorator. This means Animal cannot be instantiated directly because sound is not implemented. Next, we create a subclass Dog that inherits from Animal and implements the sound method returning 'Woof!'. When we instantiate Dog, it succeeds because all abstract methods are implemented. Calling dog.sound() returns 'Woof!'. If we try to instantiate Animal directly, Python raises a TypeError because abstract methods are not implemented. This mechanism ensures subclasses follow a required interface.