PCAP @final decorator Practice Question
A team is using inheritance but wants to prevent a method from being overridden in subclasses. What Python feature can enforce this?
⚠ Common exam trap
Candidates often think that `@final` is unenforceable or not part of Python, but since Python 3.8 it is available in the `typing` module and is the standard way to declare a method final. Conversely, the double underscore prefix is frequently misconstrued as making a method impossible to override, but it is simply a naming convention that can be bypassed.
Answer choices
Why each option matters
Answer the question above first, then reveal the full breakdown to understand why each option is right or wrong.
Correct answer & explanation
✓
Use a @final decorator.
The `@final` decorator, introduced in Python 3.8 via the `typing` module, is specifically intended to mark a method as final, signaling that it should not be overridden in subclasses. While Python does not enforce this at runtime, type checkers (e.g., mypy) can flag violations, making it the correct feature for preventing overriding in a design intent context. Option A, the double underscore prefix, only triggers name mangling (`_ClassName__method`), which is meant to avoid accidental collisions but does not prevent a subclass from defining a method with the mangled name directly, thus it does not enforce non-overridability.
Answer analysis
Option-by-option breakdown
For each option: why learners choose it and why it is or isn't the right answer here.
- ✗
Use a private method with double underscore prefix (__method).
Why it's wrong here
Name mangling only renames __method to _Class__method; subclasses can still define and call that mangled name, so overriding is not prevented. It is tempting because double underscores signal privacy, and it would be correct when merely discouraging external access, not blocking override.
- ✓
Use a @final decorator.
Why this is correct
Correct. The `@final` decorator from the `typing` module is designed to indicate that a method should not be overridden. Although not enforced at runtime, it is recognized by static type checkers and is the standard Python feature for this purpose.
- ✗
Use @staticmethod.
Why it's wrong here
@staticmethod only removes the implicit self argument; it does not restrict overriding, so subclasses can still redefine the method. It is intended for utility methods that do not access instance or class state, not for enforcing inheritance constraints.
- ✗
Use @abstractmethod.
Why it's wrong here
@abstractmethod marks a method that subclasses must implement; it does not block overriding, so a subclass can still replace it freely. It is tempting because it controls subclass behaviour, but the correct mechanism is @final (or __init_subclass__ checks), which raises an error when a subclass attempts to override the method.
Go deeper
Related to this question
About these practice questions
One of 421 original PCAP practice questions on Courseiva, each with a full explanation and wrong-answer analysis — not exam dumps or protected exam content. Learn why practice questions differ from exam dumps →
JA
Written by Johnson Ajibi, MSc IT Security
Senior Network & Security Engineer · founder of Courseiva
This PCAP practice question is part of Courseiva's free Python Institute certification practice question bank. Courseiva provides original exam-style practice questions with explanations, topic-based practice, mock exams, readiness tracking, and study analytics to help learners prepare for the PCAP exam.