When using the Template Method pattern, which Python feature prevents subclasses from overriding specific steps of the algorithm?
Trap 1: Use the @abstractmethod decorator
This forces overriding rather than preventing it.
Trap 2: Use the @staticmethod decorator
Static methods can still be overridden in subclasses.
Trap 3: Use the private keyword
Python does not have a private keyword.
- A
Use the @final decorator from the typing module
The @final decorator (available from Python 3.8+) marks a method to prevent it from being overridden in subclasses.
- B
Use the @abstractmethod decorator
Why wrong: This forces overriding rather than preventing it.
- C
Use the @staticmethod decorator
Why wrong: Static methods can still be overridden in subclasses.
- D
Use the private keyword
Why wrong: Python does not have a private keyword.